4bf49204e1550ddde34540eb4e78b3bc999ae54d
[logcheck] / logcheck.pl
1 #!/usr/bin/perl
2
3
4 require 'logcheck.conf';
5 $mode="run";
6
7
8 sub head() {
9         print "\n";
10         print "-----------------------------\n";
11         print "This is logcheck.pl V1.0.2\n";
12         print "http://peters-webcorner.de\n";
13         print "project hosted on origo\n";
14         print "http://logcheck.origo.ethz.ch\n";
15         print "-----------------------------\n\n";
16 }
17
18 if (($ARGV[0] ne "") && ($ARGV[0] ne "debug")) {
19         head();
20         print "you can use 'logcheck.pl debug' or 'logcheck.pl' without any parameters\n";
21         print "hint: using debug will prevent script from sending mail...\n";
22         exit 0;
23
24 }
25
26 if ($ARGV[0] eq "debug") {
27         head();
28         print "debug mode on...\n";
29         $mode="debug";
30
31 }
32
33 if (-e $file_whitelist) {
34         if($mode eq "debug") {
35                 print "whitelist found...\n";
36         }
37
38 else 
39 {
40         open(ADR, ">$file_whitelist");
41         print ADR "";
42         close(ADR);
43         print "Please edit ".$file_whitelist." first...\n";
44         exit 1;
45               
46 }
47 if (-e $file_logfilelist) {
48         if($mode eq "debug") {
49                 print "list of logfiles found...\n";
50         }
51 } else {
52         open(ADR, ">$file_logfilelist");
53         print ADR "";
54         close(ADR);
55         print "Please edit ".$file_logfilelist." first...\n";
56         exit 1;
57               
58 }
59
60
61 $read=0;
62 open(ADR, "<$file_whitelist");
63 while(<ADR>)
64 {
65         chop($_);
66         if(length($_) > 1) {
67                 if (substr($_,0,1) ne "#") 
68                 {
69                 $read++;
70                 push @whitelisted, $_;
71                 }
72         }
73 }
74 close(ADR);
75 if ($read > 0) {
76         if($mode eq "debug") {
77                 print $read." entries in whitelist found\n";
78         }
79 }
80 else
81 {
82         if($mode eq "debug") {
83                 print "no entries in whitelist found, may be not normal...\n";
84         }
85 }
86
87
88
89 $read=0;
90 open(ADR, "<$file_logfilelist");
91 while(<ADR>)
92 {
93         chop($_);
94         if(length($_) > 1) {
95                 if (substr($_,0,1) ne "#") 
96                 {
97                 $read++;
98                 push @logfiles, $_;
99                 }       
100         }
101 }
102 close(ADR);
103 if ($read > 0) {
104         if($mode eq "debug") {
105                 print $read." entries in logfile list found\n";
106         }
107 }
108 else
109 {
110         print "there must be at least one entry in "..$file_logfilelist."\n";
111         print "ABORTING NOW!!!\n";
112         exit 1;
113 }
114
115
116 foreach $thisfile (@logfiles) {
117         $outtext="";
118         $noffset="";
119         $offset;
120         $jumpover;
121         if($mode eq "debug") {
122                 print "processing ".$thisfile."\n";
123         }
124         if(-e $thisfile) 
125         {
126                 $useoffset=0;
127                 $thisoffset="";
128                 if(-e $thisfile.".offset") {
129                         if($mode eq "debug") {
130                                 print "using ".$thisfile.".offset\n";
131                         }
132                         $useoffset=1;
133                         open(OFF,"<$thisfile.offset");
134                         while(<OFF>)
135                         {
136                                 $offset=$_;
137                                 if($mode eq "debug") {
138                                         print "offset is $_\n"; 
139                                 }
140                         }
141                         close(OFF);
142                 }       
143                 $jumpover=1;    
144                 check();
145                 if($jumpover==1) {
146                         unlink($thisfile.".offset");
147                         if($mode eq "debug") {
148                                 print "offset not found, reparsing without offset\n";   
149                         }
150                         $jumpover=0;
151                         $offset="";
152                         check();
153                 }
154                 if ($outtext ne "") {
155                         if($mode eq "debug") {
156                                 print "mail not sent, cause debug is enabled\n";        
157                                 print "content of mail to $emailaddress would be:\n---------------------------------\n";
158                                 print $outtext;
159                                 print "\n---------------------------------\nend of mail\n";
160                         } else {
161                                 $Jetztwert = time();
162                                 $Jetztzeit = localtime($Jetztwert);
163                                 $mailer = '/usr/sbin/sendmail';
164                                 $Sender = $senderaddress;
165                                 open(MAIL, "|$mailer -t") || die "Can't open $mailer!\n";
166                                 print MAIL "To: ".$emailaddress."\n";
167                                 print MAIL "Subject: ($thisfile) violation report $Jetztzeit\n\n\n";
168                                 print MAIL $outtext;
169                                 close(MAIL);
170                         }
171                 } else {
172                         if($mode eq "debug") {
173                                 print "nothing to send, $thisfile seems to be ok\n";    
174                         }
175                 }
176                 if ($noffset ne "") {
177                         if($mode eq "debug") {
178                                 print "new offset written in ".$thisfile.".offset\n";   
179                         }
180                         open(ADR, ">$thisfile.offset");
181                                print ADR $noffset;
182                         close(ADR);
183                 }
184         }
185         else
186         {
187                 print STDERR "logfile $thisfile not found...ignoring\n";
188         }       
189 }
190 exit 0;
191
192
193 sub check() {
194         # checks the logfile itself
195         open(LOG,"<$thisfile");
196         while(<LOG>) 
197         {
198                 if ($jumpover == 0) {
199                         $wl=0;
200                         foreach $wltext (@whitelisted) 
201                         {
202                                 if($_ =~/$wltext/) 
203                                 {
204                                         $wl=1;
205                                 }
206                         }
207                         if($wl==0) 
208                         {
209                                 $outtext=$outtext.$_;
210                         }
211                 }       
212                 $noffset = substr($_,0,15,);
213                 if(substr($_,0,15) eq $offset) {
214                         $jumpover=0;
215                         if($mode eq "debug") {
216                                 print "offset found\n"; 
217                         }
218                 }
219         }
220         close(LOG);
221 }
222
223