save patch_cords.dia
[racktables-contribs] / mail_expirations.php
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 *
8 * This script requires RackTables version 0.20.11 or newer to work.
9 */
10
11 $script_mode = TRUE;
12 require '/usr/local/racktables/wwwroot/inc/init.php';
13
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
24 defineIfNotDefined ('MAILEXPR_TO', 'Admin <admin@example.com>');
25 defineIfNotDefined ('MAILEXPR_FROM', 'RackTables <racktables@example.com>');
26 defineIfNotDefined ('MAILEXPR_SUBJ', 'RackTables expirations report');
27 defineIfNotDefined ('MAILEXPR_DAYS_BEHIND', -365);
28 defineIfNotDefined ('MAILEXPR_DAYS_AHEAD', 30);
29
30 $mail_text = getExpirationsText();
31 if ($mail_text != '')
32 mail (MAILEXPR_TO, MAILEXPR_SUBJ, $mail_text, 'From: ' . MAILEXPR_FROM);
33 exit (0);
34
35 function getExpirationsText()
36 {
37 global $expirations;
38 $row_format = "%3s|%-30s|%-15s|%-15s|%s\n";
39 $ret = '';
40 $breakdown = array();
41 foreach ($expirations as $attr_id => $sections)
42 {
43 $tmp = array();
44 foreach ($sections as $section)
45 if ($section['to'] <= MAILEXPR_DAYS_AHEAD && $section['from'] >= MAILEXPR_DAYS_BEHIND)
46 $tmp[] = $section;
47 if (count ($tmp))
48 $breakdown[$attr_id] = $tmp;
49 }
50 $attrmap = getAttrMap();
51 foreach ($breakdown as $attr_id => $sections)
52 {
53 $ret .= $attrmap[$attr_id]['name'] . "\n";
54 $ret .= "===========================================\n";
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
62 $ret .= $section['title'] . "\n";
63 $ret .= "-----------------------------------------------------------------------------------\n";
64 $ret .= sprintf ($row_format, '#', 'Name', 'Asset Tag', 'OEM S/N 1', 'Date Warranty Expires');
65 $ret .= "-----------------------------------------------------------------------------------\n";
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 }
81 $ret .= "-----------------------------------------------------------------------------------\n";
82 }
83 $ret .= "\n";
84 }
85 return $ret;
86 }