File:
0.04.26a/server/bin/tester.sh (
View as Code)
1: #!/usr/bin/php -q
2: 3: /* -------------------------------------------------------------
4: This file is part of FreeNATS
5:
6: FreeNATS is (C) Copyright 2008 PurplePixie Systems
7:
8: FreeNATS is free software: you can redistribute it and/or modify
9: it under the terms of the GNU General Public License as published by
10: the Free Software Foundation, either version 3 of the License, or
11: (at your option) any later version.
12:
13: FreeNATS is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU General Public License for more details.
17:
18: You should have received a copy of the GNU General Public License
19: along with FreeNATS. If not, see www.gnu.org/licenses
20:
21: For more information see www.purplepixie.org/freenats
22: -------------------------------------------------------------- */
23:
24: if ((isset($argc))&&(isset($argv)))
25: {
26: if ($argc>1)
27: {
28: $nfilter=$argv[1];
29: }
30: else $nfilter="";
31: }
32:
33: require("include.php");
34:
35: $dbt="";
36:
37: function db($txt,$nl=true)
38: {
39: global $dbt;
40: echo $txt;
41: $dbt.=$txt;
42: if ($nl)
43: {
44: echo "\n";
45: $dbt.="
\n";
46: }
47: }
48:
49: $NATS->Start();
50: if ($nfilter!="") $st=": Node ".$nfilter;
51: else $st="";
52: db("NATS Tester Script Starting".$st);
53:
54: $highalertlevel=-1;
55: $talertc=0;
56:
57: // check if already running
58: $cq="SELECT startx FROM fntestrun WHERE fnode=\"".ss($nfilter)."\" AND finishx=0";
59: $cr=$NATS->DB->Query($cq);
60: if ($NATS->DB->Num_Rows($cr)>0)
61: {
62: $NATS->Event("Tester Already Running: Aborted",1,"Tester","Error");
63: db("Tester Already Running: Aborted");
64: $NATS->Stop();
65: exit();
66: }
67:
68: $gq="INSERT INTO fntestrun(startx,fnode) VALUES(".time().",\"".ss($nfilter)."\")";
69: $NATS->DB->Query($gq);
70: $trid=$NATS->DB->Insert_Id();
71: db("Test ID: ".$trid." (Started at ".nicedt(time()).")");
72: $NATS->Event("Tester ".$trid." Started",5,"Tester","Start");
73:
74: db(" ");
75:
76: if ($nfilter=="") $q="SELECT * FROM fnnode WHERE nodeenabled=1";
77: else
78: {
79: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nfilter)."\" AND nodeenabled=1";
80: }
81: $r=$NATS->DB->Query($q);
82:
83:
84: while ($row=$NATS->DB->Fetch_Array($r))
85: {
86: $dotests=true;
87: $alertlevel=0;
88: $alerts=array();
89: $alertc=0;
90: db("NodeID: ".$row['nodeid']);
91: $NATS->Event("Tester ".$trid." Node ".$row['nodeid'],10,"Tester","Node");
92:
93: // Scheduling Test In Here - sets dotests to false and alertlevel to -1 untested
94: if ($row['scheduleid']!=0) // has a schedule
95: {
96: db(" Has Schedule: Yes - Checking");
97: $run=run_x_in_schedule(time(),$row['scheduleid']);
98: if (!$run)
99: {
100: db(" In Schedule: No - Skipping Tests");
101: $NATS->Event("Tester ".$trid." Skipped by Schedule",5,"Tester","Node");
102: $dotests=false;
103: $alertlevel=-1;
104: }
105: else db(" In Schedule: Yes");
106: }
107:
108:
109: $ptr=0;
110: $pal=0;
111:
112: if ($dotests) // record running now
113: {
114: $q="UPDATE fnnode SET lastrunx=".time()." WHERE nodeid=\"".ss($row['nodeid'])."\"";
115: $NATS->DB->Query($q);
116: }
117:
118: if ($row['pingtest']&&$dotests)
119: {
120: db(" Ping Test: Yes");
121: $NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping");
122: $ptr=PingTest($row['hostname']);
123: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
124: db(" Ping Returned: ".$ptr);
125: if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.attempts","2")>1) )
126: {
127: $att=$NATS->Cfg->Get("test.icmp.attempts","2");
128: for ($a=2; $a<=$att; $a++) // starting on second attempt
129: {
130: // try again...
131: test_sleep();
132: db(" Trying Ping Again - X".$a);
133: $NATS->Event("Tester ".$trid." Ping X".$a." Node ".$row['nodeid'],10,"Tester","Ping");
134: $ptr=PingTest($row['hostname']);
135: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
136: db(" Ping Returned: ".$ptr);
137: if ($ptr>0) $a=$att+1; // break out of the loop
138: }
139: }
140:
141: if ($ptr<=0)
142: {
143: $alertlevel=2;
144: db(" Ping Test: Failed");
145: $alerts[$alertc++]="ping failed";
146: $pal=2;
147: }
148: else db(" Ping Test: Passed");
149:
150: // pingtest output bodge
151: // is there a test entry for ICMP
152: $fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
153: $fr=$NATS->DB->Query($fq);
154: $ltid_icmp="";
155: if ($irow=$NATS->DB->Fetch_Array($fr))
156: { // exists
157: $uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time().",lastvalue=".$ptr." WHERE localtestid=".$irow['localtestid'];
158: $ltid_icmp=$irow['localtestid'];
159: //echo $uq;
160: $NATS->DB->Query($uq);
161: }
162: else
163: { // doesn't exist
164: $uq="INSERT INTO fnlocaltest(nodeid,testtype,alertlevel,lastrunx,lastvalue) VALUES(\"".$row['nodeid']."\",\"ICMP\",".$pal.",".time().",".$ptr.")";
165: //echo $uq;
166: $NATS->DB->Query($uq);
167: $ltid_icmp=$NATS->DB->Insert_Id();
168: }
169: $NATS->DB->Free($fr);
170:
171: // record the ICMP bodge-test here
172: $rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")";
173: $NATS->DB->Query($rq);
174: //echo $rq." ".$NATS->DB->Affected_Rows()."\n";
175:
176: }
177: else
178: { // further ICMP bodge - update to -1 or do nothing if the test doesn't exist
179: $uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
180: $NATS->DB->Query($uq);
181: }
182:
183: if ($dotests&&($row['pingfatal'])&&($ptr<=0))
184: {
185: db(" Ping Fatal: Yes - Not Continuing");
186: $NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping");
187: $dotests=false;
188: }
189:
190: // do the tests - only actually exec if dotests true
191:
192: $first_test=true;
193:
194: db("Doing Local Tests");
195: $NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test");
196: $q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\" AND testenabled=1 ORDER BY localtestid ASC";
197: $res=$NATS->DB->Query($q);
198: while ($lrow=$NATS->DB->Fetch_Array($res))
199: {
200: if ($first_test)
201: {
202: $first_test=false;
203: if ($row['pingtest']==1) test_sleep(); // sleep if has done a ping
204: }
205: else test_sleep();
206:
207: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")");
208:
209: // Build parameter array
210: $params=array();
211: for ($a=1; $a<10; $a++)
212: {
213: $parstr="testparam".$a;
214: $params[$a]=$lrow[$parstr];
215: }
216:
217: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
218: if ($dotests) $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params);
219: else $result=0;
220: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
221: db(" Result: ".$result);
222:
223: if ($dotests)
224: {
225: // evaluation
226: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result);
227: else $lvl=nats_eval("L".$lrow['localtestid'],$result);
228: db(" Eval: ".$lvl);
229:
230: // put in the custom retries based on attempts here - we KNOW dotests is on so don't need to worry about untested status
231: $att=$lrow['attempts'];
232: if ( ($lvl!=0) && (is_numeric($att)) && ($att>1) )
233: {
234: for ($a=2; $a<=$att; $a++)
235: {
236: test_sleep();
237: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].") X".$a);
238: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." X".$a." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
239: $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params);
240: db(" Result: ".$result);
241: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result);
242: else $lvl=nats_eval("L".$lrow['localtestid'],$result);
243: db(" Eval: ".$lvl);
244: if ($lvl==0) $a=$att+1; // test passed
245: }
246: }
247:
248: // $lvl is now the last lvl regardless of where it came from
249:
250: if ($lvl>$alertlevel) $alertlevel=$lvl;
251: if ($lvl>0)
252: {
253: if ($lrow['testname']=="") $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." ";
254: else $s=$lrow['testname']." ";
255: if ($lvl>1) $s.="failed";
256: else $s.="warning";
257: $alerts[$alertc++]=$s;
258: }
259: } else $lvl=-1;
260:
261: // record it
262: if ($lrow['testrecord']==1)
263: {
264: $tid="L".$lrow['localtestid'];
265: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES(";
266: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")";
267: $NATS->DB->Query($iq);
268: db(" Recording Test");
269: }
270: if ((!isset($result))||(!is_numeric($result))) $result=0; // safety net
271:
272: // update localtest record
273: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl.",lastvalue=".$result." WHERE localtestid=".$lrow['localtestid'];
274: $NATS->DB->Query($uq);
275:
276:
277: }
278:
279:
280:
281: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node");
282:
283:
284: db("Highest Alert Level: ".$alertlevel);
285: db("Alert Count : ".$alertc);
286: $als="";
287: foreach($alerts as $al) $als.=$al.", ";
288: db("Alerts: ".$als);
289:
290: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts);
291:
292: db(" ");
293:
294: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel;
295: $talertc+=$alertc;
296:
297: }
298:
299:
300:
301: db("Finished Tests... Finishing Off");
302: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc);
303: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid;
304: $NATS->DB->Query($uq);
305:
306:
307: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat");
308: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop");
309:
310: // in here for now...
311: $NATS->ActionFlush();
312:
313: $NATS->Stop();
314: db("NATS Stopped... Finished");
315: ?>
316:
317: