File:
0.02.53a/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: require("include.php");
33: $dbt="";
34: function db($txt,$nl=true)
35: {
36: global $dbt;
37: echo $txt;
38: $dbt.=$txt;
39: if ($nl)
40: {
41: echo "\n";
42: $dbt.="
\n";
43: }
44: }
45: $NATS->Start();
46: if ($nfilter!="") $st=": Node ".$nfilter;
47: else $st="";
48: db("NATS Tester Script Starting".$st);
49:
50: $highalertlevel=-1;
51: $talertc=0;
52:
53: // check if already running
54: $cq="SELECT startx FROM fntestrun WHERE fnode=\"".ss($nfilter)."\" AND finishx=0";
55: $cr=$NATS->DB->Query($cq);
56: if ($NATS->DB->Num_Rows($cr)>0)
57: {
58: $NATS->Event("Tester Already Running: Aborted",1,"Tester","Error");
59: db("Tester Already Running: Aborted");
60: $NATS->Stop();
61: exit();
62: }
63:
64: $gq="INSERT INTO fntestrun(startx,fnode) VALUES(".time().",\"".ss($nfilter)."\")";
65: $NATS->DB->Query($gq);
66: $trid=$NATS->DB->Insert_Id();
67: db("Test ID: ".$trid." (Started at ".nicedt(time()).")");
68: $NATS->Event("Tester ".$trid." Started",5,"Tester","Start");
69:
70: db(" ");
71:
72: if ($nfilter=="") $q="SELECT * FROM fnnode WHERE nodeenabled=1";
73: else
74: {
75: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nfilter)."\" AND nodeenabled=1";
76: }
77: $r=$NATS->DB->Query($q);
78:
79:
80: while ($row=$NATS->DB->Fetch_Array($r))
81: {
82: $dotests=true;
83: $alertlevel=0;
84: $alerts=array();
85: $alertc=0;
86: db("NodeID: ".$row['nodeid']);
87: $NATS->Event("Tester ".$trid." Node ".$row['nodeid'],10,"Tester","Node");
88:
89: $ptr=0;
90: $pal=0;
91: if ($row['pingtest'])
92: {
93: db(" Ping Test: Yes");
94: $NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping");
95: $ptr=PingTest($row['hostname']);
96: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
97: db(" Ping Returned: ".$ptr);
98: if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.trytwice","1")=="1") )
99: {
100: // try again...
101: db(" Trying Ping Again");
102: $NATS->Event("Tester ".$trid." Ping X2 Node ".$row['nodeid'],10,"Tester","Ping");
103: $ptr=PingTest($row['hostname']);
104: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
105: db(" Ping Returned: ".$ptr);
106: }
107:
108: if ($ptr<=0)
109: {
110: $alertlevel=2;
111: db(" Ping Test: Failed");
112: $alerts[$alertc++]="ping failed";
113: $pal=2;
114: }
115: else db(" Ping Test: Passed");
116:
117: // pingtest output bodge
118: // is there a test entry for ICMP
119: $fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
120: $fr=$NATS->DB->Query($fq);
121: $ltid_icmp="";
122: if ($irow=$NATS->DB->Fetch_Array($fr))
123: { // exists
124: $uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time()." WHERE localtestid=".$irow['localtestid'];
125: $ltid_icmp=$irow['localtestid'];
126: //echo $uq;
127: $NATS->DB->Query($uq);
128: }
129: else
130: { // doesn't exist
131: $uq="INSERT INTO fnlocaltest(nodeid,testtype,alertlevel,lastrunx) VALUES(\"".$row['nodeid']."\",\"ICMP\",".$pal.",".time().")";
132: //echo $uq;
133: $NATS->DB->Query($uq);
134: $ltid_icmp=$NATS->DB->Insert_Id();
135: }
136: $NATS->DB->Free($fr);
137:
138: // record the ICMP bodge-test here
139: $rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")";
140: $NATS->DB->Query($rq);
141: //echo $rq." ".$NATS->DB->Affected_Rows()."\n";
142:
143: }
144: else
145: { // further ICMP bodge - update to -1 or do nothing if the test doesn't exist
146: $uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
147: $NATS->DB->Query($uq);
148: }
149:
150: if (($row['pingfatal'])&&($ptr<=0))
151: {
152: db(" Ping Fatal: Yes - Not Continuing");
153: $NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping");
154: $dotests=false;
155: }
156:
157: // do the tests - only actually exec if dotests true
158:
159: db("Doing Local Tests");
160: $NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test");
161: $q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\"";
162: $res=$NATS->DB->Query($q);
163: while ($lrow=$NATS->DB->Fetch_Array($res))
164: {
165: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")");
166: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
167: if ($dotests) $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname']);
168: else $result=0;
169: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
170: db(" Result: ".$result);
171:
172: if ($dotests)
173: {
174: // evaluation
175: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result);
176: else $lvl=nats_eval("L".$lrow['localtestid'],$result);
177:
178: db(" Eval: ".$lvl);
179: if ($lvl>$alertlevel) $alertlevel=$lvl;
180: if ($lvl>0)
181: {
182: $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." ";
183: if ($lvl>1) $s.="failed";
184: else $s.="warning";
185: $alerts[$alertc++]=$s;
186: }
187: } else $lvl=-1;
188:
189: // record it
190: if ($lrow['testrecord']==1)
191: {
192: $tid="L".$lrow['localtestid'];
193: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES(";
194: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")";
195: $NATS->DB->Query($iq);
196: db(" Recording Test");
197: }
198:
199: // update localtest record
200: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl." WHERE localtestid=".$lrow['localtestid'];
201: $NATS->DB->Query($uq);
202:
203:
204: }
205:
206:
207:
208: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node");
209:
210:
211: db("Highest Alert Level: ".$alertlevel);
212: db("Alert Count : ".$alertc);
213: $als="";
214: foreach($alerts as $al) $als.=$al.", ";
215: db("Alerts: ".$als);
216:
217: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts);
218:
219: db(" ");
220:
221: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel;
222: $talertc+=$alertc;
223:
224: }
225:
226:
227:
228: db("Finished Tests... Finishing Off");
229: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc);
230: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid;
231: $NATS->DB->Query($uq);
232:
233:
234: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat");
235: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop");
236:
237: // in here for now...
238: $NATS->ActionFlush();
239:
240: $NATS->Stop();
241: db("NATS Stopped... Finished");
242: ?>
243:
244: