demo: simplify demoreload.sh
[racktables-contribs] / local_link2nagios.php
1 <?php
2 //
3 // Local file for a link with Nagios & LiveStatus
4 // Copyright 2010 Jeroen Benda <Jeroen.Benda@tomtom.com>
5 //
6 // Variables needed:
7 // $livestatusServer = The server where LiveStatus is running (the Nagios server)
8 // $livestatusServerPort = Port where LiveStatus listens on
9 // $nagiosURL = The full url to link to a server in Nagios (variable replacement %x% can be used)
10 // $noNagiosCheck = array of object type id codes that do not require a Nagios check (i.e. patch panel and such)
11 //
12
13 $tab['object']['Link2Nagios'] = 'Nagios';
14 $trigger['object']['Link2Nagios'] = 'localtrigger_Link2Nagios';
15 $tabhandler['object']['Link2Nagios'] = 'localfunc_Link2Nagios';
16
17 $trigger['renderRack']['state'] = 'formtrigger_NagiosRack';
18
19 //
20 // Constants used for LiveStatus status results
21 //
22 define("_statusNagiosOK",0); // green
23 define("_statusNagiosWarning",1); // yellow
24 define("_statusNagiosCritical",2); // red
25 define("_statusNagiosUnknown",3); // orange
26
27 define("_statusNagiosOKText","ok");
28 define("_statusNagiosWarningText","warning");
29 define("_statusNagiosCriticalText","critical");
30 define("_statusNagiosUnknownText","unknown");
31
32 //
33 // This function is called when a tab is clicked
34 // It forwards to Nagios for that server
35 //
36 function localfunc_Link2Nagios()
37 {
38 global $nagiosURL;
39 assertUIntArg('object_id',__FUNCTION__);
40 $object = spotEntity('object',$_REQUEST['object_id']);
41 forwardToURL(str_replace(array("%objectname%"),array($object['name']),$nagiosURL));
42 }
43
44 //
45 // This function determines whether a host exists in Nagios through a LiveStatus query
46 // If it does, the color returned is the color of the status of that object in Nagios
47 //
48 function localtrigger_Link2Nagios()
49 {
50 global $noNagiosCheck;
51 assertUIntArg('object_id',__FUNCTION__);
52 $object = spotEntity('object',$_REQUEST['object_id']);
53 if (!in_array($object['objtype_id'],$noNagiosCheck)) {
54 $trigger = getNagiosState($object['name']);
55 return $trigger;
56 } else {
57 return '';
58 }
59 }
60
61 //
62 // The function the sends a LiveStatus query to the server and returns the result given
63 //
64 function getLiveStatusQuery ($xQuery) {
65 global $livestatusServer, $livestatusServerPort, $allLiveStatusResults;
66 $allLiveStatusResults = array();
67 $theCommand = "echo -e \"{$xQuery}\" | netcat {$livestatusServer} {$livestatusServerPort}";
68 $temp = exec($theCommand,$allLiveStatusResults,$a1);
69 if (strlen($temp)>0 && $temp{0}=="-") {
70 $temp = "";
71 }
72 return $temp;
73 }
74
75 //
76 // This function uses LiveStatus to determine whether a host exists and Nagios and if so
77 // returns the status as text
78 //
79 function getNagiosState ($xName) {
80 $nagiosState = "";
81 $temp1 = getLiveStatusQuery("GET hosts\nColumns: state\nFilter: name = {$xName}");
82 if (strlen($temp1)>0) {
83 if ($temp1>0) {
84 $nagiosState = _statusNagiosUnknownText;
85 } else {
86 $temp = getLiveStatusQuery(
87 "GET services\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3\nFilter: host_name = {$xName}");
88 $temp2 = explode(";",$temp);
89 if (count($temp2)<3) {
90 $nagiosState = _statusNagiosUnknownText;
91 } else {
92 if ($temp2[_statusNagiosCritical]>0) {
93 $nagiosState = _statusNagiosCriticalText;
94 } else if ($temp2[_statusNagiosWarning]>0) {
95 $nagiosState = _statusNagiosWarningText;
96 } else if ($temp2[_statusNagiosUnknown]>0) {
97 $nagiosState = _statusNagiosUnknownText;
98 } else {
99 $nagiosState = _statusNagiosOKText;
100 }
101 }
102 }
103 }
104 return $nagiosState;
105 }
106
107 //
108 // This function determines the color of the item in the rack layout
109 // based on the state of the object in Nagios
110 //
111 function formtrigger_NagiosRack ($locItem) {
112 global $noNagiosCheck;
113 $state = $locItem['state'];
114 if ($state=="T") {
115 $temp = spotEntity('object',$locItem['object_id']);
116 if (strlen($temp['name'])>0 && !in_array($temp['objtype_id'],$noNagiosCheck)) {
117 $nagiosstate = getNagiosState($temp['name']);
118 if (strlen($nagiosstate)!=0) {
119 $state = $nagiosstate;
120 }
121 }
122 }
123 return $state;
124 }