save patch_cords.dia
[racktables-contribs] / mail_expirations.php
CommitLineData
1e1c482c
DO
1#!/usr/bin/env php
2<?php
3
4/* This script produces a plain-text version of the expirations report and
5 * emails it to the specified address unless the report contains no objects.
6 * It is intended to be run from command line as daily or weekly cron job.
7 *
c8945a01 8 * This script requires RackTables version 0.20.11 or newer to work.
1e1c482c
DO
9 */
10
1e1c482c
DO
11$script_mode = TRUE;
12require '/usr/local/racktables/wwwroot/inc/init.php';
13
c8945a01
DO
14/*
15 * init.php will include any plugins/*.php files, that would be the right place
16 * to pre-define any of the constants below like:
17 *
18 * define ('MAILEXPR_TO', 'User <user@example.com>');
19 *
20 * This makes it possible to leave this script in its original form and
21 * (hopefully) simplify the future upgrades.
22 */
23
24defineIfNotDefined ('MAILEXPR_TO', 'Admin <admin@example.com>');
25defineIfNotDefined ('MAILEXPR_FROM', 'RackTables <racktables@example.com>');
26defineIfNotDefined ('MAILEXPR_SUBJ', 'RackTables expirations report');
936eda58 27defineIfNotDefined ('MAILEXPR_DAYS_BEHIND', -365);
c8945a01
DO
28defineIfNotDefined ('MAILEXPR_DAYS_AHEAD', 30);
29
1e1c482c
DO
30$mail_text = getExpirationsText();
31if ($mail_text != '')
c8945a01 32 mail (MAILEXPR_TO, MAILEXPR_SUBJ, $mail_text, 'From: ' . MAILEXPR_FROM);
1e1c482c
DO
33exit (0);
34
35function getExpirationsText()
36{
c8945a01 37 global $expirations;
01f049cf 38 $row_format = "%3s|%-30s|%-15s|%-15s|%s\n";
1e1c482c
DO
39 $ret = '';
40 $breakdown = array();
c8945a01
DO
41 foreach ($expirations as $attr_id => $sections)
42 {
43 $tmp = array();
44 foreach ($sections as $section)
936eda58 45 if ($section['to'] <= MAILEXPR_DAYS_AHEAD && $section['from'] >= MAILEXPR_DAYS_BEHIND)
c8945a01
DO
46 $tmp[] = $section;
47 if (count ($tmp))
48 $breakdown[$attr_id] = $tmp;
49 }
1e1c482c
DO
50 $attrmap = getAttrMap();
51 foreach ($breakdown as $attr_id => $sections)
52 {
01f049cf
DO
53 $ret .= $attrmap[$attr_id]['name'] . "\n";
54 $ret .= "===========================================\n";
1e1c482c
DO
55 foreach ($sections as $section)
56 {
57 $count = 1;
58 $result = scanAttrRelativeDays ($attr_id, $section['from'], $section['to']);
59 if (! count ($result))
60 continue;
61
01f049cf
DO
62 $ret .= $section['title'] . "\n";
63 $ret .= "-----------------------------------------------------------------------------------\n";
1e1c482c 64 $ret .= sprintf ($row_format, '#', 'Name', 'Asset Tag', 'OEM S/N 1', 'Date Warranty Expires');
01f049cf 65 $ret .= "-----------------------------------------------------------------------------------\n";
1e1c482c
DO
66 foreach ($result as $row)
67 {
68 $object = spotEntity ('object', $row['object_id']);
69 $attributes = getAttrValues ($object['id']);
70 $ret .= sprintf
71 (
72 $row_format,
73 $count,
74 $object['dname'],
75 $object['asset_no'],
76 array_key_exists (1, $attributes) ? $attributes[1]['a_value'] : '',
77 datetimestrFromTimestamp ($row['uint_value'])
78 );
79 $count++;
80 }
01f049cf 81 $ret .= "-----------------------------------------------------------------------------------\n";
1e1c482c 82 }
01f049cf 83 $ret .= "\n";
1e1c482c
DO
84 }
85 return $ret;
86}