jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [lib/] [datasources/] [WeatherMapDataSource_cactithold.php] - Blame information for rev 95

 

Line No. Rev Author Line
185simandl<?php
2 
3// Cacti thold/monitor DS plugin
4// Can read state of Thresholds from the THold Cacti plugin
5// and also overall host state, in the style of the Monitor plugin (it doesn't depend on that plugin to do this)
6//
7// It DOES depend on THold though, obviously!
8//
9// Possible TARGETs:
10//
11// cactithold:234
12// (internal thold id - returns 0 for OK, and 1 for breach)
13//
14// cactithold:12:444
15// (the two IDs seen in thold URLs- also returns 0 for OK, and 1 for breach)
16//
17// cactimonitor:22
18// (cacti hostid - returns host state (0-3) or 4 for failing some thresholds)
19// also sets all the same variables as cactihost: would, and a new possible 'state' name of 'tholdbreached'
20//
21// Original development for this plugin was paid for by
22// Stellar Consulting
23 
24class WeatherMapDataSource_cactithold extends WeatherMapDataSource {
25 
26 function Init(&$map)
27 {
28 global $plugins;
29 
30 if($map->context == 'cacti')
31 {
32 if( !function_exists('db_fetch_row') )
33 {
34 debug("ReadData CactiTHold: Cacti database library not found. [THOLD001]\n");
35 return(FALSE);
36 }
37 
38$thold_present = false;
39 
40 if (function_exists("api_plugin_is_enabled")) {
41 if (api_plugin_is_enabled('thold')) {
42 $thold_present = true;
43 }
44 }
45 
46 if ( isset($plugins) && in_array('thold',$plugins)) {
47 $thold_present = true;
48 }
49 
50 if ( !$thold_present) {
51 debug("ReadData CactiTHold: THold plugin not enabled. [THOLD002]\n");
52 }
53 
54 $sql = "show tables";
55 $result = db_fetch_assoc($sql) or die (mysql_error());
56 $tables = array();
57 
58 foreach($result as $index => $arr) {
59 foreach ($arr as $t) {
60 $tables[] = $t;
61 }
62 }
63 
64 if( !in_array('thold_data', $tables) )
65 {
66 debug('ReadData CactiTHold: thold_data database table not found. [THOLD003]\n');
67 return(FALSE);
68 }
69 
70 return(TRUE);
71 }
72 else
73 {
74 debug("ReadData CactiTHold: Can only run from Cacti environment. [THOLD004]\n");
75 }
76 
77 return(FALSE);
78 }
79 
80 function Recognise($targetstring)
81 {
82 if(preg_match("/^cacti(thold|monitor):(\d+)$/",$targetstring,$matches))
83 {
84 return TRUE;
85 }
86 elseif(preg_match("/^cactithold:(\d+):(\d+)$/",$targetstring,$matches))
87 {
88 return TRUE;
89 }
90 else
91 {
92 return FALSE;
93 }
94 }
95 
96 function ReadData($targetstring, &$map, &$item)
97 {
98 
99 $data[IN] = NULL;
100 $data[OUT] = NULL;
101 $data_time = 0;
102 
103 if(preg_match("/^cactithold:(\d+):(\d+)$/",$targetstring,$matches))
104 {
105 // Returns 0 if threshold is not breached, 1 if it is.
106 // use target aggregation to build these up into a 'badness' percentage
107 // takes the same two values that are visible in thold's own URLs (the actual thold ID isn't shown anywhere)
108 
109 $rra_id = intval($matches[1]);
110 $data_id = intval($matches[2]);
111 
112 $SQL2 = "select thold_alert from thold_data where rra_id=$rra_id and data_id=$data_id and thold_enabled='on'";
113 $result = db_fetch_row($SQL2);
114 if(isset($result))
115 {
116 if($result['thold_alert'] > 0) { $data[IN]=1; }
117 else { $data[IN] = 0; }
118 $data[OUT] = 0;
119 }
120 }
121 elseif(preg_match("/^cacti(thold|monitor):(\d+)$/",$targetstring,$matches))
122 {
123 $type = $matches[1];
124 $id = intval($matches[2]);
125 
126 if($type=='thold')
127 {
128 // VERY simple. Returns 0 if threshold is not breached, 1 if it is.
129 // use target aggregation to build these up into a 'badness' percentage
130 $SQL2 = "select thold_alert from thold_data where id=$id and thold_enabled='on'";
131 $result = db_fetch_row($SQL2);
132 if(isset($result))
133 {
134 if($result['thold_alert'] > 0) { $data[IN]=1; }
135 else { $data[IN] = 0; }
136 $data[OUT] = 0;
137 }
138 }
139 
140 if($type=='monitor')
141 {
142 debug("CactiTHold ReadData: Getting cacti basic state for host $id\n");
143 $SQL = "select * from host where id=$id";
144 
145 // 0=disabled
146 // 1=down
147 // 2=recovering
148 // 3=up
149 // 4=tholdbreached
150 
151 $state = -1;
152 $statename = '';
153 $result = db_fetch_row($SQL);
154 if(isset($result))
155 {
156 // create a note, which can be used in icon filenames or labels more nicely
157 if($result['status'] == 1) { $state = 1; $statename = 'down'; }
158 if($result['status'] == 2) { $state = 2; $statename = 'recovering'; }
159 if($result['status'] == 3) { $state = 3; $statename = 'up'; }
160 if($result['disabled']) { $state = 0; $statename = 'disabled'; }
161 
162 $data[IN] = $state;
163 $data[OUT] = 0;
164 $item->add_note("state",$statename);
165 $item->add_note("cacti_description",$result['description']);
166 
167 $item->add_note("cacti_hostname",$result['hostname']);
168 $item->add_note("cacti_curtime",$result['cur_time']);
169 $item->add_note("cacti_avgtime",$result['avg_time']);
170 $item->add_note("cacti_mintime",$result['min_time']);
171 $item->add_note("cacti_maxtime",$result['max_time']);
172 $item->add_note("cacti_availability",$result['availability']);
173 
174 $item->add_note("cacti_faildate",$result['status_fail_date']);
175 $item->add_note("cacti_recdate",$result['status_rec_date']);
176 }
177 debug("CactiTHold ReadData: Basic state for host $id is $state/$statename\n");
178 
179 debug("CactiTHold ReadData: Checking threshold states for host $id\n");
180 $numthresh = 0;
181 $numfailing = 0;
182 $SQL2 = "select rra_id, data_id, thold_alert from thold_data,data_local where thold_data.rra_id=data_local.id and data_local.host_id=$id and thold_enabled='on'";
183 # $result = db_fetch_row($SQL2);
184 $queryrows = db_fetch_assoc($SQL2);
185 if( is_array($queryrows) )
186 {
187 foreach ($queryrows as $th) {
188 $desc = $th['rra_id']."/".$th['data_id'];
189 $v = $th['thold_alert'];
190 $numthresh++;
191 if(intval($th['thold_alert']) > 0)
192 {
193 debug("CactiTHold ReadData: Seen threshold $desc failing ($v)for host $id\n");
194 $numfailing++;
195 }
196 else
197 {
198 debug("CactiTHold ReadData: Seen threshold $desc OK ($v) for host $id\n");
199 }
200 }
201 }
202 else
203 {
204 debug("CactiTHold ReadData: Failed to get thold info for host $id\n");
205 }
206 
207 debug("CactiTHold ReadData: Checked $numthresh and found $numfailing failing\n");
208 
209 if( ($numfailing > 0) && ($numthresh > 0) && ($state==3) )
210 {
211 $state = 4;
212 $statename = "tholdbreached";
213 $item->add_note("state",$statename);
214 $item->add_note("thold_failcount",$numfailing);
215 $item->add_note("thold_failpercent",($numfailing/$numthresh)*100);
216 $data[IN] = $state;
217 $data[OUT] = $numfailing;
218 debug("CactiTHold ReadData: State is $state/$statename\n");
219 }
220 elseif( $numthresh>0 )
221 {
222 $item->add_note("thold_failcount",0);
223 $item->add_note("thold_failpercent",0);
224 debug("CactiTHold ReadData: Leaving state as $state\n");
225 }
226 }
227 }
228 
229 debug ("CactiTHold ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n");
230 
231 return( array($data[IN], $data[OUT], $data_time) );
232 }
233}
234 
235 
236// vim:ts=4:sw=4:
237?>

Powered by WebSVN 2.2.1