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