summaryrefslogtreecommitdiff
path: root/unit1.pas
blob: 7d9672cd239f39350780220793aa213629a2c35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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.