Merge pull request #27 from bpothier/patch-2
[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 *
8 * Tested to work with RackTables 0.20.8.
9 */
10
11$mail_to = 'Admin <admin@example.com>';
12$mail_from = 'RackTables <racktables@example.com>';
13$mail_subject = 'RackTables expirations report';
14
15
16$script_mode = TRUE;
17require '/usr/local/racktables/wwwroot/inc/init.php';
18
19$mail_text = getExpirationsText();
20if ($mail_text != '')
21 mail ($mail_to, $mail_subject, $mail_text, "From: ${mail_from}");
22exit (0);
23
24function getExpirationsText()
25{
26 $row_format = "%3s|%-30s|%-15s|%-15s|%s\r\n";
27 $ret = '';
28 $breakdown = array();
29 $breakdown[21] = array
30 (
31 array ('from' => -365, 'to' => 0, 'title' => 'has expired within last year'),
32 array ('from' => 0, 'to' => 30, 'title' => 'expires within 30 days'),
33// array ('from' => 30, 'to' => 60, 'title' => 'expires within 60 days'),
34// array ('from' => 60, 'to' => 90, 'title' => 'expires within 90 days'),
35 );
36 $breakdown[22] = $breakdown[21];
37 $breakdown[24] = $breakdown[21];
38 $attrmap = getAttrMap();
39 foreach ($breakdown as $attr_id => $sections)
40 {
41 $ret .= $attrmap[$attr_id]['name'] . "\r\n";
42 $ret .= "===========================================\r\n";
43 foreach ($sections as $section)
44 {
45 $count = 1;
46 $result = scanAttrRelativeDays ($attr_id, $section['from'], $section['to']);
47 if (! count ($result))
48 continue;
49
50 $ret .= $section['title'] . "\r\n";
51 $ret .= "-----------------------------------------------------------------------------------\r\n";
52 $ret .= sprintf ($row_format, '#', 'Name', 'Asset Tag', 'OEM S/N 1', 'Date Warranty Expires');
53 $ret .= "-----------------------------------------------------------------------------------\r\n";
54 foreach ($result as $row)
55 {
56 $object = spotEntity ('object', $row['object_id']);
57 $attributes = getAttrValues ($object['id']);
58 $ret .= sprintf
59 (
60 $row_format,
61 $count,
62 $object['dname'],
63 $object['asset_no'],
64 array_key_exists (1, $attributes) ? $attributes[1]['a_value'] : '',
65 datetimestrFromTimestamp ($row['uint_value'])
66 );
67 $count++;
68 }
69 $ret .= "-----------------------------------------------------------------------------------\r\n";
70 }
71 $ret .= "\r\n";
72 }
73 return $ret;
74}
75
76?>