unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, Komponenten; type { TForm1 } TForm1 = class(TForm) gruenIcon: TImage; gelbIcon: TImage; rotIcon: TImage; najaGruenIcon: TImage; Memo1: TMemo; TrayIcon1: TTrayIcon; procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure FormCreate(Sender: TObject); procedure FormResize(Sender: TObject); procedure FormShow(Sender: TObject); procedure TrayIcon1DblClick(Sender: TObject); procedure TrayIcon1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { private declarations } lmb: tMouseButton; procedure readOptions(nam: string); public { public declarations } logDateiName: string; pruefListe: array of tTestAufgabe; procedure statusAenderung(sender: tObject); procedure log(s: string); end; var form1: tForm1; implementation uses lowlevelunit; {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject); begin lmb:=mbLeft; logDateiName:='/tmp/watchdog.log'; setlength(pruefListe,0); readOptions(extractFilePath(application.exeName)+'tests.konf'); statusAenderung(self); form1.visible:=false; end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); var i: longint; begin for i:=0 to length(pruefListe)-1 do pruefListe[i].threadBeenden; end; procedure TForm1.FormResize(Sender: TObject); begin memo1.width:=form1.clientWidth; memo1.height:=form1.clientHeight; end; const __erstesMal: boolean = true; procedure TForm1.FormShow(Sender: TObject); begin if __erstesMal then begin __erstesMal:=false; form1.visible:=false; end; form1.left:=screen.monitors[0].left+(screen.monitors[0].width-form1.width) div 2; form1.top:=screen.monitors[0].top+(screen.monitors[0].height-form1.height-24) div 2; end; procedure TForm1.TrayIcon1DblClick(Sender: TObject); begin case lmb of mbLeft: form1.visible:=not form1.visible; mbRight: form1.close; end{of case}; end; procedure TForm1.TrayIcon1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin lmb:=button; end; procedure tForm1.readOptions(nam: string); var f: textFile; s,t: string; bol: boolean; begin assignFile(F,nam); reset(f); while not eof(f) do begin readln(f,s); if (length(s)=0) or (s[1]='#') then continue; setlength(pruefListe,length(pruefListe)+1); t:=erstesArgument(s,#9); bol:=(leftStr(t,1)='(') and (rightStr(t,1)=')'); if bol then t:=copy(t,2,length(t)-2); if t='ip' then pruefListe[length(pruefliste)-1]:=tTestIp.create else if t='ping' then pruefListe[length(pruefListe)-1]:=tTestPing.create else if t='remcmp' then pruefListe[length(pruefListe)-1]:=tRemoteFileCompare.create else if t='remdate' then pruefListe[length(pruefListe)-1]:=tRemoteFileDate.create else begin log('Unbekannter Test '''+t+'''.'); setlength(pruefListe,length(pruefListe)-1); continue; end; pruefListe[length(pruefListe)-1].ernst:=not bol; pruefListe[length(pruefListe)-1].log:=@form1.log; t:=erstesArgument(s,#9); pruefListe[length(pruefListe)-1].bedingung:=loeseLogischenTerm(t); t:=erstesArgument(s,#9); pruefListe[length(pruefListe)-1].okIntervall:=strToInt(t); t:=erstesArgument(s,#9); pruefListe[length(pruefListe)-1].fehlerIntervall:=strToInt(t); t:=erstesArgument(s,#9); pruefListe[length(pruefListe)-1].fehlerNachricht:=t; if not pruefListe[length(pruefListe)-1].nimmParameter(s) then raise exception.create('Unverständliche Parameter: '''+s+'''!'); pruefListe[length(pruefListe)-1].statusAenderung:=@form1.statusAenderung; end; closeFile(f); end; procedure TForm1.statusAenderung(sender: TObject); var i,j: longint; stati: array of boolean; s: string; begin setlength(stati,length(pruefListe)); for i:=0 to length(pruefListe)-1 do stati[i]:=pruefListe[i].status = 1; for i:=0 to length(pruefListe)-1 do pruefListe[i].aktivieren(stati); j:=0; for i:=0 to length(pruefListe)-1 do if pruefListe[i].bedingungErfuellt(stati) then if pruefListe[i].status<>1 then j:=j or (1 shl (pruefListe[i].status + byte((pruefListe[i].status=0) and pruefListe[i].ernst))); memo1.lines.clear; if j=0 then begin s:= 'Alles OK!'#0; form1.icon:=gruenIcon.picture.icon; end else begin s:=''; if odd(j shr 1) then begin form1.icon:=rotIcon.picture.icon; j:=8; end else if odd(j shr 2) then begin form1.icon:=gelbIcon.picture.icon; j:=14; end else begin form1.icon:=najaGruenIcon.picture.icon; j:=4; end; For i:=0 to length(pruefListe)-1 do if pruefListe[i].bedingungErfuellt(stati) then begin if (j mod 4 = pruefListe[i].status) and odd(j shr (2+Byte(pruefListe[i].ernst))) then begin s:=s + ','#$0D#$0A + pruefListe[i].fehlerNachricht; memo1.lines.add(pruefListe[i].fehlerNachricht); end else memo1.lines.add('nicht: '+pruefListe[i].fehlerNachricht); end; s:=s+'!'#0; delete(s,1,3); end; trayIcon1.hint:=s; trayIcon1.icon:=form1.icon; trayIcon1.visible:=true; end; procedure TForm1.log(s: string); var f: textFile; begin assignFile(f,logDateiName); if fileExists(logDateiName) then append(f) else rewrite(f); writeln(f,dateTimeToStr(now)+': '+s); closeFile(f); end; end.