forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report-em_switch_exceptions.php
135 lines (114 loc) · 5.6 KB
/
report-em_switch_exceptions.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
require_once( 'db.inc.php' );
require_once( 'facilities.inc.php' );
require_once( 'swiftmailer/swift_required.php' );
// If any port other than 25 is specified, assume encryption and authentication
if($config->ParameterArray['SMTPPort']!= 25){
$transport=Swift_SmtpTransport::newInstance()
->setHost($config->ParameterArray['SMTPServer'])
->setPort($config->ParameterArray['SMTPPort'])
->setEncryption('ssl')
->setUsername($config->ParameterArray['SMTPUser'])
->setPassword($config->ParameterArray['SMTPPassword']);
}else{
$transport=Swift_SmtpTransport::newInstance()
->setHost($config->ParameterArray['SMTPServer'])
->setPort($config->ParameterArray['SMTPPort']);
}
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::NewInstance()->setSubject( __("Data Center Switch Capacity Exceptions Report" ) );
// Set from address
try{
$message->setFrom($config->ParameterArray['MailFromAddr']);
}catch(Swift_RfcComplianceException $e){
$error.=__("MailFrom").": <span class=\"errmsg\">".$e->getMessage()."</span><br>\n";
}
// Add data center team to the list of recipients
try{
$message->addTo($config->ParameterArray['FacMgrMail']);
}catch(Swift_RfcComplianceException $e){
$error.=__("Facility Manager email address").": <span class=\"errmsg\">".$e->getMessage()."</span><br>\n";
}
$logo=getcwd().'/images/'.$config->ParameterArray["PDFLogoFile"];
$logo=$message->embed(Swift_Image::fromPath($logo)->setFilename('logo.png'));
$htmlMessage = sprintf( "<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>ITS Data Center Inventory</title></head><body><div id=\"header\" style=\"padding: 5px 0;background: %s;\"><center><img src=\"%s\"></center></div><div class=\"page\"><p>\n", $config->ParameterArray["HeaderColor"], $logo );
$htmlMessage .= sprintf( "<p>The following switches are near full capacity per documentation.</p>" );
$devList = Device::GetSwitchesToReport();
$lastDC = null;
$lastCabinet = null;
$urlBase = $config->ParameterArray["InstallURL"];
$threshold = intval($config->ParameterArray["NetworkThreshold"]) / 100;
if ( sizeof( $devList ) == 0 ) {
$htmlMessage .= "<p>There are no switches that qualify for this report.</p>\n";
} else {
$cab = new Cabinet();
$dc = new DataCenter();
$port = new DevicePorts();
$dev = new Device();
$exceptionRows = "";
$mismatchRows = "";
foreach ( $devList as $devRow ) {
if ( $devRow->Cabinet != $lastCabinet ) {
$cab->CabinetID = $devRow->Cabinet;
$cab->GetCabinet();
$lastCabinet = $cab->CabinetID;
$cabinet = $cab->Location;
}
if ( $cab->DataCenterID != $lastDC ) {
$dc->DataCenterID = $cab->DataCenterID;
$dc->GetDataCenter();
$lastDC = $dc->DataCenterID;
$dataCenter = $dc->Name;
}
$port->DeviceID = $devRow->DeviceID;
$activeCount = intval( $port->getActivePortCount() );
$portList = $port->getPorts();
$statusList = SwitchInfo::getPortStatus( $devRow->DeviceID );
if ( sizeof( $statusList ) == sizeof( $portList ) && ( sizeof( $portList ) > 0 ) ) {
for ( $n = 0; $n <= sizeof( $portList ); $n++ ) {
// The return from getPorts() is not a sequential array, it's associative, so you can't iterate through by number
$currPort = array_shift( $portList );
if ( ( $statusList[$n+1] == "up" && ( $currPort->Notes=="" && $currPort->ConnectedDeviceID == null )) || ( $statusList[$n+1] == "down" && ( $currPort->Notes!="" || $currPort->ConnectedDeviceID != null )) ) {
if ( $currPort->ConnectedDeviceID > 0 ) {
$dev->DeviceID = $currPort->ConnectedDeviceID;
$dev->GetDevice();
$devAnchor = "<a href=\"" . $urlBase . "devices.php?DeviceID=" . $dev->DeviceID . "\">" . $dev->Label . "</a>";
$port->DeviceID = $currPort->ConnectedDeviceID;
$port->PortNumber =$currPort->ConnectedPort;
$port->getPort();
$portName = $port->Label;
} else {
$devAnchor = " ";
$portName = " ";
}
$exceptionRows .= sprintf( "<tr><td><a href=\"%sdevices.php?DeviceID=%d\">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $urlBase, $devRow->DeviceID, $devRow->Label, $currPort->Label, $devAnchor, $portName, $currPort->Notes, $statusList[$n+1] );
}
}
}
if ( $activeCount >= floor( $devRow->Ports * $threshold ) ) {
$mismatchRows .= sprintf( "<tr><td>%s</td><td>%s</td><td><a href=\"%sdevices.php?DeviceID=%d\">%s</a></td><td>%d</td><td>%d</td></tr>\n", $dataCenter, $cabinet, $urlBase, $devRow->DeviceID, $devRow->Label, $devRow->Ports, $activeCount );
$dataCenter = " ";
$cabinet = " ";
}
}
if ( $mismatchRows != "" ) {
$htmlMessage .= "<table border='1'>\n<tr><th>Data Center</th><th>Cabinet</th><th>Device Name</th><th>Total Ports</th><th>Documented Ports</th></tr>\n";
$htmlMessage .= $mismatchRows;
$htmlMessage .= "</table>\n";
}
if ( $exceptionRows != "" ) {
$htmlMessage .= "<p>The following ports specifically have an exception between documentation and SNMP information.</p>";
$htmlMessage .= "<table border='1'>\n<tr><th>Device Name</th><th>Port</th><th>Documented Device</th><th>Documented Port</th><th>Notes</th><th>Link Status</th></tr>\n";
$htmlMessage .= $exceptionRows;
$htmlMessage .= "</table>\n";
}
}
$message->setBody($htmlMessage,'text/html');
try {
$result = $mailer->send( $message );
} catch( Swift_RfcComplianceException $e) {
$error .= "Send: " . $e->getMessage() . "<br>\n";
} catch( Swift_TransportException $e) {
$error .= "Server: <span class=\"errmsg\">" . $e->getMessage() . "</span><br>\n";
}
?>