Release of grains plugin to racktables-contribs
[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_AHEAD', 30);
28
29 $mail_text = getExpirationsText();
30 if ($mail_text != '')
31 mail (MAILEXPR_TO, MAILEXPR_SUBJ, $mail_text, 'From: ' . MAILEXPR_FROM);
32 exit (0);
33
34 function getExpirationsText()
35 {
36 global $expirations;
37 $row_format = "%3s|%-30s|%-15s|%-15s|%s\r\n";
38 $ret = '';
39 $breakdown = array();
40 foreach ($expirations as $attr_id => $sections)
41 {
42 $tmp = array();
43 foreach ($sections as $section)
44 if ($section['to'] <= MAILEXPR_DAYS_AHEAD)
45 $tmp[] = $section;
46 if (count ($tmp))
47 $breakdown[$attr_id] = $tmp;
48 }
49 $attrmap = getAttrMap();
50 foreach ($breakdown as $attr_id => $sections)
51 {
52 $ret .= $attrmap[$attr_id]['name'] . "\r\n";
53 $ret .= "===========================================\r\n";
54 foreach ($sections as $section)
55 {
56 $count = 1;
57 $result = scanAttrRelativeDays ($attr_id, $section['from'], $section['to']);
58 if (! count ($result))
59 continue;
60
61 $ret .= $section['title'] . "\r\n";
62 $ret .= "-----------------------------------------------------------------------------------\r\n";
63 $ret .= sprintf ($row_format, '#', 'Name', 'Asset Tag', 'OEM S/N 1', 'Date Warranty Expires');
64 $ret .= "-----------------------------------------------------------------------------------\r\n";
65 foreach ($result as $row)
66 {
67 $object = spotEntity ('object', $row['object_id']);
68 $attributes = getAttrValues ($object['id']);
69 $ret .= sprintf
70 (
71 $row_format,
72 $count,
73 $object['dname'],
74 $object['asset_no'],
75 array_key_exists (1, $attributes) ? $attributes[1]['a_value'] : '',
76 datetimestrFromTimestamp ($row['uint_value'])
77 );
78 $count++;
79 }
80 $ret .= "-----------------------------------------------------------------------------------\r\n";
81 }
82 $ret .= "\r\n";
83 }
84 return $ret;
85 }
86
87 ?>