init III
This commit is contained in:
733
Perl CGI C2/c2.pl
Normal file
733
Perl CGI C2/c2.pl
Normal file
@@ -0,0 +1,733 @@
|
||||
#!/bin/perl
|
||||
|
||||
package PerlSvc;
|
||||
|
||||
use Mysql;
|
||||
use Net::SNMP;
|
||||
use Net::Ping;
|
||||
use BER;
|
||||
|
||||
require 'SNMP_Session.pm';
|
||||
|
||||
our ($c2,$dbh, $stopim);
|
||||
our %Config;
|
||||
|
||||
$PerlSvc::Name = "C2MYSQL";
|
||||
$PerlSvc::DisplayName = "C2MYSQL";
|
||||
|
||||
sub Startup {
|
||||
my $start=2;
|
||||
#$thr1 = threads->new(\&check);
|
||||
while(my $run = PerlSvc::ContinueRun($c2->{'conf'}->{'check_intervall'})) { #$run bei stop auf false
|
||||
if ($start > 0) { # bei ersten 2 starts keine checks damit service keinen timeout bekommt
|
||||
$stopim = 0; # nicht stoppen
|
||||
$start--;
|
||||
}
|
||||
elsif ($run) { # wenn $run true / nicht false checks durchführen
|
||||
#$thr1->join();
|
||||
check();
|
||||
}
|
||||
else { # wenn nicht erster start ($start==0) und $run false (stop received)
|
||||
$stopim = 1; # für stop sorgen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub wl {
|
||||
#open LOGF, ">>c:/c2-mysql.log";
|
||||
#print LOGF "$_[0]\n";
|
||||
#close LOGF;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
check();
|
||||
sleep 10;
|
||||
}
|
||||
|
||||
sub check {
|
||||
my ($dbusr,$dbpwd,$dbcstr);
|
||||
wl("open c2.ini");
|
||||
open INI, "<c:\\c2.ini" or die "Kann c:\\c2.ini nicht finden!\n";
|
||||
wl("read c2.ini");
|
||||
while (<INI>) {
|
||||
chomp;
|
||||
($dbusr,$dbpwd,$dbcstr) = split /#/,$_;
|
||||
}
|
||||
wl("close c2.ini");
|
||||
close INI;
|
||||
wl("connect 2 db");
|
||||
$dbh = DBI->connect("$dbcstr","$dbusr","$dbpwd", {RaiseError => 1});
|
||||
#while (1) {
|
||||
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
wl("read db");
|
||||
read_database();
|
||||
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
wl("get time");
|
||||
$c2->{'time'} = get_time();
|
||||
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
wl("get date");
|
||||
$c2->{'date'} = get_date();
|
||||
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
if(run_now()) {
|
||||
wl("get service values");
|
||||
foreach my $todo (keys %{$c2->{'todo'}}) {
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
wl("get service value");
|
||||
get_service_value($todo);
|
||||
}
|
||||
#open TEST, ">TEST.txt";
|
||||
#print TEST Dumper ($c2);
|
||||
#close TEST;
|
||||
if ($stopim) { wl("stop received"); exit; }
|
||||
wl("create webpage");
|
||||
create_web_from_template();
|
||||
}
|
||||
#print "\n";
|
||||
#}
|
||||
}
|
||||
|
||||
sub run_now {
|
||||
my $should_run=1;
|
||||
my %ts;
|
||||
|
||||
($ts{"ss"},$ts{"mm"},$ts{"hh"},$ts{"dd"},$ts{"mon"},$ts{"yy"},$ts{"we"},$ts{"doy"},$ts{"st"})=localtime;
|
||||
$ts{'yy'}+=1900; $ts{'mon'}++;
|
||||
#print "$ts{'yy'} $ts{'mon'} $ts{'dd'} $ts{'we'}\n";
|
||||
|
||||
my $sth = $dbh->prepare("select value from run_on where day='$ts{'we'}'");
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
if ($ref->[0] == 0) {
|
||||
$should_run=0;
|
||||
}
|
||||
}
|
||||
#my $datum = "$ts{'yy'}"."-"."$ts{'mon'}"."-"."$ts{'dd'}";
|
||||
my $datum = "$ts{'yy'}"."-";
|
||||
if ($ts{'mon'} <10 ) {
|
||||
$datum=$datum ."0$ts{'mon'}"."-";
|
||||
}
|
||||
else {
|
||||
$datum=$datum . "$ts{'mon'}"."-";
|
||||
}
|
||||
if ($ts{'dd'} <10 ) {
|
||||
$datum=$datum . "0$ts{'dd'}";
|
||||
}
|
||||
else {
|
||||
$datum=$datum . "$ts{'dd'}";
|
||||
}
|
||||
#print "## $datum\n";
|
||||
my $sel="select description from not_run_on where datum='$datum'";
|
||||
#print "### $sel\n";
|
||||
my $sth = $dbh->prepare($sel);
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
#print "#### $ref->[0]\n";
|
||||
if ($ref->[0] ne "") {
|
||||
$should_run=0;
|
||||
}
|
||||
}
|
||||
|
||||
my $sel="select start_time from conf where active = '1'";
|
||||
my $sth = $dbh->prepare($sel);
|
||||
my $start_time;
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
$start_time=$ref->[0];
|
||||
}
|
||||
my ($stah,$stam,$stas);
|
||||
($stah,$stam,$stas) = split /:/,$start_time;
|
||||
my $start_timestamp=$stah*3600 + $stam*60 + $stas;
|
||||
|
||||
my $sel="select stop_time from conf where active = '1'";
|
||||
my $sth = $dbh->prepare($sel);
|
||||
my $stop_time;
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
$stop_time=$ref->[0];
|
||||
}
|
||||
my ($stoh,$stom,$stos);
|
||||
($stoh,$stom,$stos) = split /:/,$stop_time;
|
||||
my $stop_timestamp=$stoh*3600 + $stom*60 + $stos;
|
||||
|
||||
my $akt_timestamp = $ts{'hh'}*3600 + $ts{'mm'}*60 + $ts{'ss'};
|
||||
if ( ($akt_timestamp < $start_timestamp) or ($akt_timestamp > $stop_timestamp) ) {
|
||||
$should_run = 0;
|
||||
}
|
||||
|
||||
|
||||
return $should_run;
|
||||
}
|
||||
|
||||
sub clear_values {
|
||||
foreach my $td (keys %{$c2->{'todo'}}) {
|
||||
foreach my $posi (keys %{$c2->{'todo'}->{$td}->{'services'}}) {
|
||||
$c2->{'todo'}->{$td}->{'services'}->{$posi}->{'ergebnis'}="";
|
||||
$c2->{'todo'}->{$td}->{'services'}->{$posi}->{'error'}=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub create_web_from_template {
|
||||
my ($zeile_orig,$zeile,$host,$td,$count);
|
||||
open IN, "<$c2->{'conf'}->{'web_template'}";
|
||||
open OUT, ">$c2->{'conf'}->{'index_dir'}";
|
||||
if ($c2->{'conf'}->{'web_enable'}==1) {
|
||||
$count=0;
|
||||
while (<IN>) {
|
||||
my $metarefresh = "<meta http-equiv='refresh' content='$c2->{'conf'}->{'web_refresh_intervall'}; $c2->{'conf'}->{'web_link'}'>";
|
||||
if (/\[C2REPEAT\]/) {
|
||||
s/\[C2REPEAT\]//;
|
||||
|
||||
my $when=0; # 0:immer 1:Nur bei Fehler 2:Nur wenn kein Fehler
|
||||
if (/DisplayError/) { $when = 1; }
|
||||
elsif (/DisplayGood/) { $when = 2; }
|
||||
elsif (/DisplayAll/) { $when = 0; }
|
||||
s/DisplayError//;
|
||||
s/DisplayGood//;
|
||||
s/DisplayAll//;
|
||||
|
||||
$zeile_orig=$_;
|
||||
foreach $td (sort keys %{$c2->{'todo'}}) {
|
||||
if (display("$td","$when")) {
|
||||
$zeile = $zeile_orig;
|
||||
#$host = $c2->{'todo'}->{$td}->{'hosts'}->{'hostname'};
|
||||
if ($count==1) {
|
||||
$zeile =~ s/<tr>/<tr bgcolor='a0ffa0'>/;
|
||||
$count = 0;
|
||||
}
|
||||
else {
|
||||
$zeile =~ s/<tr>/<tr bgcolor='ffffa0'>/;
|
||||
$count = 1;
|
||||
}
|
||||
#$zeile =~ s/\[C2HOSTNAME\]/$c2->{'todo'}->{$td}->{'hosts'}->{'hostname'}/;
|
||||
$zeile =~ s/\[C2HOSTNAME\]/<a href='$c2->{'conf'}->{'config_url'}edit_host\.pl\?edit=$c2->{'todo'}->{$td}->{'hosts'}->{'ind'}&edit1=Edit' target='konfig'>$c2->{'todo'}->{$td}->{'hosts'}->{'hostname'}<\/a>/;
|
||||
#<a href='admin\/edit_host\.pl\?edit=$c2->{'todo'}->{$td}->{'hosts'}->{'ind'}&edit1=Edit' target='new'>$c2->{'todo'}->{$td}->{'hosts'}->{'hostname'}<\/a>/;
|
||||
$zeile =~ s/\[C2HOSTIP\]/$c2->{'todo'}->{$td}->{'hosts'}->{'ip'}/;
|
||||
$zeile =~ s/\[C2HOSTDESCRIPTION\]/<a href='$c2->{'conf'}->{'config_url'}edit_host\.pl\?edit=$c2->{'todo'}->{$td}->{'hosts'}->{'ind'}&edit1=Edit' target='konfig'>$c2->{'todo'}->{$td}->{'hosts'}->{'description'}<\/a>/;
|
||||
|
||||
$zeile =~ s/\[C2CHECKIF\]/$c2->{'todo'}->{$td}->{'checks'}->{'beding'}/;
|
||||
$zeile =~ s/\[C2CHECKVALUE\]/$c2->{'todo'}->{$td}->{'checks'}->{'wert'}/;
|
||||
$zeile =~ s/\[C2CHECKDESCRIPTION\]/<a href='$c2->{'conf'}->{'config_url'}edit_check\.pl?edit=$c2->{'todo'}->{$td}->{'checks'}->{'ind'}&edit1=Edit' target='konfig'>$c2->{'todo'}->{$td}->{'checks'}->{'description'}<\/a>/;
|
||||
|
||||
$zeile =~ s/\[C2ALERTTYPE\]/$c2->{'todo'}->{$td}->{'alerts'}->{'alert_type'}/;
|
||||
$zeile =~ s/\[C2ALERTDESCRIPTION\]/<a href='$c2->{'conf'}->{'config_url'}edit_alert\.pl?edit=$c2->{'todo'}->{$td}->{'alerts'}->{'ind'}&edit1=Edit' target='konfig'>$c2->{'todo'}->{$td}->{'alerts'}->{'description'}<\/a>/;
|
||||
|
||||
foreach my $sid (keys %{$c2->{'todo'}->{$td}->{'services'}}) {
|
||||
if ($c2->{'todo'}->{$td}->{'services'}->{$sid}->{'todo'} eq "R") {
|
||||
if ($c2->{'todo'}->{$td}->{'services'}->{$sid}->{'error'}=='1') {
|
||||
$zeile =~ s/\[C2ERROR\]/<font color='FF0000'><b>$c2->{'todo'}->{$td}->{'services'}->{$sid}->{'error'}<\/b><\/font>/;
|
||||
}
|
||||
else {
|
||||
$zeile =~ s/\[C2ERROR\]/<font color='00FF00'><b>$c2->{'todo'}->{$td}->{'services'}->{$sid}->{'error'}<\/b><\/font>/;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $sid (keys %{$c2->{'todo'}->{$td}->{'services'}}) {
|
||||
if ($c2->{'todo'}->{$td}->{'services'}->{$sid}->{'todo'} eq "R") {
|
||||
$zeile =~ s/\[C2SERVICENAME\]/$c2->{'todo'}->{$td}->{'services'}->{$sid}->{'description'}/;
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $sid (keys %{$c2->{'todo'}->{$td}->{'services'}}) {
|
||||
if ($c2->{'todo'}->{$td}->{'services'}->{$sid}->{'todo'} eq "R") {
|
||||
my $erg = $c2->{'todo'}->{$td}->{'services'}->{$sid}->{'ergebnis'};
|
||||
unless ($erg =~ /[a-zA-Z]/) {
|
||||
$erg =~ s/\./,/;
|
||||
$erg =~ s/(.*,[0-9]{0,2})[0-9]*/$1/;
|
||||
}
|
||||
$zeile =~ s/\[C2RESULT\]/$erg/;
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $sid (keys %{$c2->{'todo'}->{$td}->{'services'}}) {
|
||||
if ($c2->{'todo'}->{$td}->{'services'}->{$sid}->{'todo'} eq "R") {
|
||||
$zeile =~ s/\[C2SERVICETIME\]/$c2->{'todo'}->{$td}->{'services'}->{$sid}->{'check_time'}/;
|
||||
}
|
||||
}
|
||||
|
||||
print OUT "$zeile";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
s/\[C2METAREFRESH\]/$metarefresh/;
|
||||
s/\[C2CONFIGURL\]/$c2->{'conf'}->{'config_url'}/;
|
||||
print OUT "$_";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
print OUT "<html>\n<head>\n";
|
||||
print OUT "<meta http-equiv='refresh' content='$c2->{'conf'}->{'web_refresh_intervall'}; $c2->{'conf'}->{'web_link'}'>\n";
|
||||
print OUT "</head>\n<body>\n";
|
||||
print OUT "Webanzeige ist ausgeschaltet!<br><br>\n";
|
||||
print OUT "<a href='$c2->{'conf'}->{'config_url'}' target='konfig'>Konfiguration & Administration</a>\n";
|
||||
print OUT "</body>\n</html>";
|
||||
}
|
||||
close IN;
|
||||
close OUT;
|
||||
}
|
||||
|
||||
sub display {
|
||||
my ($todo,$when) = @_;
|
||||
# $when 0:immer 1:Nur bei Fehler 2:Nur wenn kein Fehler soll angezeigt werden!
|
||||
my $ret;
|
||||
foreach my $sid (keys %{$c2->{'todo'}->{$todo}->{'services'}}) {
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{$sid}->{'todo'} eq "R") {
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{$sid}->{'error'}=='1') {
|
||||
$ret = 1 if ($when == 0 or $when == 1);
|
||||
$ret = 0 if ($when == 2);
|
||||
}
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{$sid}->{'error'}=='0') {
|
||||
$ret = 1 if ($when == 0 or $when == 2);
|
||||
$ret = 0 if ($when == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub get_service_value {
|
||||
my ($todo)=@_;
|
||||
my ($va1,$va2,$service_error);
|
||||
foreach my $position (sort keys %{$c2->{'todo'}->{$todo}->{'services'}}) {
|
||||
$service_error = 0;
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'todo'} eq "S") {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = get_snmp_value($todo,$position);
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'todo'} eq "P") {
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} eq "IP") {
|
||||
if (ping ($c2->{'todo'}->{$todo}->{'hosts'}->{'ip'})) {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = 1;
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 0;
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'error'}->{2}->{'error_text'};
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 1;
|
||||
}
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} eq "HOST") {
|
||||
if (ping ($c2->{'todo'}->{$todo}->{'hosts'}->{'hostname'})) {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = 1;
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 0;
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'error'}->{2}->{'error_text'};
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültiger Adresstyp. Nur IP,HOST erlaubt.");
|
||||
}
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'todo'} eq "K") {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'};
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'todo'} eq "C") {
|
||||
my $var = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'};
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} =~ /\//) {
|
||||
($va1,$va2) = split /\//,$var;
|
||||
$va1 = delete_front_back_spaces($va1);
|
||||
$va2 = delete_front_back_spaces($va2);
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'} == 0) {
|
||||
alert_log($todo,$position,"Division by 0.");
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'error'} = 1;
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$va1"}->{'ergebnis'} / $c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'};
|
||||
}
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} =~ /\*/) {
|
||||
($va1,$va2) = split /\*/,$var;
|
||||
$va1 = delete_front_back_spaces($va1);
|
||||
$va2 = delete_front_back_spaces($va2);
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$va1"}->{'ergebnis'} * $c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'};
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} =~ /\+/) {
|
||||
($va1,$va2) = split /\+/,$var;
|
||||
$va1 = delete_front_back_spaces($va1);
|
||||
$va2 = delete_front_back_spaces($va2);
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$va1"}->{'ergebnis'} + $c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'};
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'} =~ /\-/) {
|
||||
($va1,$va2) = split /\-/,$var;
|
||||
$va1 = delete_front_back_spaces($va1);
|
||||
$va2 = delete_front_back_spaces($va2);
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$va1"}->{'ergebnis'} - $c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'};
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültige Rechenart. Nur +-*/ erlaubt.");
|
||||
}
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'todo'} eq "R") {
|
||||
my $var = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'variable'};
|
||||
($va1,$va2) = split /\-/,$var;
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'old_time'} = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_time'};
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_time'} = get_timestamp();
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'check_time'} = get_time();
|
||||
$va1 = delete_front_back_spaces($va1);
|
||||
$va2 = delete_front_back_spaces($va2);
|
||||
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'old_value'} = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_value'};
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_value'} = $c2->{'todo'}->{$todo}->{'services'}->{"$va2"}->{'ergebnis'};
|
||||
|
||||
if ($va1 eq "A") {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_value'};
|
||||
}
|
||||
elsif ($va1 eq "R") {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'ergebnis'} = ($c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_value'} - $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'old_value'})/( $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'new_time'} - $c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'old_time'} );
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültige Art bei Ergebnis. Nur A und R erlaubt.");
|
||||
}
|
||||
if (check_if_error($todo,$position)) { ####
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'error'} = 1;
|
||||
send_alert ($todo,$position);
|
||||
alert_log ($todo,$position,"Beding. erfüllt, not pingable oder kein snmp-response! => Alarm wurde gesendet");
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{"$position"}->{'error'} = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültiger Check. Nur K,C,S,P,R erlaubt.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub send_alert {
|
||||
my ($todo,$position) = @_;
|
||||
if ($c2->{'todo'}->{$todo}->{'alerts'}->{'alert_type'} eq "snmp") {
|
||||
send_trap($todo);
|
||||
}
|
||||
elsif ($c2->{'todo'}->{$todo}->{'alerts'}->{'alert_type'} eq "mail") {
|
||||
;
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültiger Alarmtyp. Nur snmp und mail erlaubt.");
|
||||
}
|
||||
}
|
||||
|
||||
sub send_trap {
|
||||
my ($todo) = @_;
|
||||
my $dest_host = $c2->{'todo'}->{$todo}->{'alerts'}->{'destination'};
|
||||
my $src_host = $c2->{'todo'}->{$todo}->{'hosts'}->{'ip'};
|
||||
my $generic = $c2->{'todo'}->{$todo}->{'alerts'}->{'generic'};
|
||||
my $specific = $c2->{'todo'}->{$todo}->{'alerts'}->{'specific'};
|
||||
my $txt = $c2->{'todo'}->{$todo}->{'alerts'}->{'txt_msg'};
|
||||
my $community = $c2->{'todo'}->{$todo}->{'alerts'}->{'community'};
|
||||
my $port = $c2->{'todo'}->{$todo}->{'alerts'}->{'port'};
|
||||
my $enterprise = $c2->{'todo'}->{$todo}->{'alerts'}->{'enterprise'};
|
||||
|
||||
my @t = split /\./,$enterprise; # Traps(Parameter 'TRAP')
|
||||
|
||||
my $trap_session = SNMP_Session->open ($dest_host, $community, $port); # Destination-IP(Parameter 'D'); Community(Parameter 'C')
|
||||
$trap_session->trap_request_send(encode_oid(@t),
|
||||
encode_ip_address($src_host), #$s_ip # Source-IP(Parameter 'S')
|
||||
encode_int($generic), # Priorität(Parameter 'P') 0:coldStart 1:warmstart 2:linkdown 3:linkup 4:authenticationfailure 5:egpneighborloss 6:enterprise
|
||||
encode_int($specific), # wenn hier 0 ist.
|
||||
encode_string($txt));
|
||||
}
|
||||
|
||||
sub error_log {
|
||||
my ($todo,$position,$msg) = @_;
|
||||
open FILE, ">>$c2->{'conf'}->{'errors_log'}";
|
||||
print FILE "$c2->{'date'} $c2->{'time'} $msg Tabelle services ind# $c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ind'}\n";
|
||||
close FILE;
|
||||
}
|
||||
|
||||
sub alert_log {
|
||||
my ($todo,$position,$msg) = @_;
|
||||
my $v1=$c2->{'date'};
|
||||
my $v2=$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'check_time'};
|
||||
my $v3=$c2->{'todo'}->{$todo}->{'hosts'}->{'description'};
|
||||
my $v4=$c2->{'todo'}->{$todo}->{'hosts'}->{'hostname'};
|
||||
my $v5=$c2->{'todo'}->{$todo}->{'hosts'}->{'ip'};
|
||||
my $v6=$c2->{'todo'}->{$todo}->{'checks'}->{'description'};
|
||||
my $v7=$c2->{'todo'}->{$todo}->{'checks'}->{'beding'};
|
||||
my $v8=$c2->{'todo'}->{$todo}->{'checks'}->{'wert'};
|
||||
my $v9=$c2->{'todo'}->{$todo}->{'alerts'}->{'description'};
|
||||
my $va=$c2->{'todo'}->{$todo}->{'alerts'}->{'alert_type'};
|
||||
my $vb=$c2->{'todo'}->{$todo}->{'hosts'}->{'ind'};
|
||||
my $vc=$c2->{'todo'}->{$todo}->{'checks'}->{'ind'};
|
||||
my $vd=$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ind'};
|
||||
my $ve=$c2->{'todo'}->{$todo}->{'alerts'}->{'ind'};
|
||||
my $vf=$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'service_ind'};
|
||||
my $vg=$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'};
|
||||
$dbh->do("insert into alerts_log (date,time,host_description,host_hostname,host_ip,check_description,check_beding,check_wert,alert_description,alert_alert_type,host_ind,alert_ind,check_ind,pos_in_service,service_nr,msg,service_result) values ('$v1','$v2','$v3','$v4','$v5','$v6','$v7','$v8','$v9','$va','$vb','$ve','$vc','$position','$vf','$msg','$vg')");
|
||||
}
|
||||
|
||||
sub check_if_error {
|
||||
my ($todo,$position) = @_;
|
||||
my $error=0;
|
||||
my $more_checks = 1;
|
||||
my $ergebnis = $c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'};
|
||||
my $bed = $c2->{'todo'}->{$todo}->{'checks'}->{'beding'};
|
||||
my $value = $c2->{'todo'}->{$todo}->{'checks'}->{'wert'};
|
||||
|
||||
# alle todo-$todo-services-$posi durchlaufen
|
||||
# alle error durchlaufen
|
||||
# wenn todo-$todo-services-$posi-ergebnis eq error-$error-error_text
|
||||
# alle todo-$todo-services-$posi durchlaufen
|
||||
# wenn todo-$todo-services-$posi-todo eq R
|
||||
# todo-$todo-services-$posi-ergebnis = error-$error-error_text
|
||||
# $error=1
|
||||
# $no_more_checks=0
|
||||
|
||||
foreach my $posi1 ( keys %{$c2->{'todo'}->{$todo}->{'services'}} ) {
|
||||
foreach my $err1 ( keys %{$c2->{'error'}} ) {
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{$posi1}->{'ergebnis'} eq $c2->{'error'}->{$err1}->{'error_text'} ) {
|
||||
foreach my $posi2 ( keys %{$c2->{'todo'}->{$todo}->{'services'}} ) {
|
||||
if ($c2->{'todo'}->{$todo}->{'services'}->{$posi2}->{'todo'} eq "R") {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$posi2}->{'ergebnis'} = $c2->{'error'}->{$err1}->{'error_text'};
|
||||
$error = 1;
|
||||
$more_checks=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($more_checks) {
|
||||
if ($bed eq "LT") { if ($ergebnis < $value) {$error=1;} }
|
||||
elsif ($bed eq "NLT") { unless ($ergebnis < $value) {$error=1;} }
|
||||
elsif ($bed eq "GT") { if ($ergebnis > $value) {$error=1;} }
|
||||
elsif ($bed eq "NGT") { unless ($ergebnis < $value) {$error=1;} }
|
||||
elsif ($bed eq "EQ") { if ($ergebnis eq $value) {$error=1;} }
|
||||
elsif ($bed eq "NEQ") { if ($ergebnis ne $value) {$error=1;} }
|
||||
elsif ($bed eq "RE") { if ($ergebnis =~ /$value/) {$error=1;} }
|
||||
elsif ($bed eq "BT") {
|
||||
my ($v1,$v2) = split /\-/,$value;
|
||||
if (($ergebnis > $v1) and ($ergebnis < $v2)) {$error=1;}
|
||||
}
|
||||
elsif ($bed eq "NBT") {
|
||||
my ($v1,$v2) = split /\-/,$value;
|
||||
if (($ergebnis <= $v1) or ($ergebnis >= $v2)) {$error=1;}
|
||||
}
|
||||
else {
|
||||
alert_log($todo,$position,"Ungültiger Vergleich im Check. Nur RE,(N)LT,GT,EQ,BT erlaubt.");
|
||||
}
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
sub get_snmp_value {
|
||||
my ($todo,$position) = @_;
|
||||
my ($host,$community,$port,$oid,$result,$ret);
|
||||
my $temp = 1;
|
||||
$host = $c2->{'todo'}->{$todo}->{'hosts'}->{'ip'};
|
||||
if (ping($host)) {
|
||||
$community = $c2->{'todo'}->{$todo}->{'hosts'}->{'ro_community'};
|
||||
$port = $c2->{'todo'}->{$todo}->{'hosts'}->{'snmp_port'};
|
||||
$oid = $c2->{'todo'}->{$todo}->{'services'}->{$position}->{'variable'};
|
||||
print "### $todo, $host, $community, $port\n";
|
||||
my ($session,$error) = Net::SNMP->session(Hostname => "$host", Community => "$community", Port => $port);
|
||||
$session->retries(3);
|
||||
$result = $session->get_request("$oid");
|
||||
$session->close;
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 1;
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'} = $c2->{'error'}->{2}->{'error_text'};
|
||||
; # Not reachable
|
||||
alert_log ($todo,$position,"interner Fehler im Dienst (ping)");
|
||||
$temp=0;
|
||||
}
|
||||
if ($result->{"$oid"} eq "") {
|
||||
if ($temp) {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 1;
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'} = $c2->{'error'}->{3}->{'error_text'};
|
||||
; # no snmp response
|
||||
alert_log ($todo,$position,"interner Fehler im Dienst (snmp request)");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($temp) {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'error'} = 0;
|
||||
if ($oid =~ /1.3.6.1.2.1.1.3.0/) { # SYS_UPTIME
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'} = sys_uptime($result->{"$oid"});
|
||||
}
|
||||
else {
|
||||
$c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'} = $result->{"$oid"};
|
||||
}
|
||||
}
|
||||
}
|
||||
$ret = $c2->{'todo'}->{$todo}->{'services'}->{$position}->{'ergebnis'};
|
||||
print " $todo, $position : $host $community $port $oid $ret\n";
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub sys_uptime {
|
||||
my ($txt) = @_;
|
||||
# 24.01 seconds
|
||||
# 1 minute, 06.60
|
||||
# 2 hours, 25:19.22
|
||||
#print "$txt\n";
|
||||
$txt =~ s/[a-z ]//g;
|
||||
$txt =~ s/[.:-]/,/g;
|
||||
#print "$txt\n";
|
||||
my @array;
|
||||
@array = split /,/,$txt;
|
||||
|
||||
while (@array < 5) {
|
||||
@array = (0,@array);
|
||||
}
|
||||
|
||||
$array[0] = $array[0] * 24 * 60 * 60;
|
||||
$array[1] = $array[1] * 60 * 60;
|
||||
$array[2] = $array[2] * 60;
|
||||
my $tticks = ($array[0] + $array[1] + $array[2] + $array[3])*100 + $array[4];
|
||||
|
||||
return $tticks;
|
||||
}
|
||||
|
||||
sub ping {
|
||||
my ($host) = @_;
|
||||
my $pingtype = "icmp";
|
||||
my $timeout = 2;
|
||||
my $bytes = 32;
|
||||
my $ok = 0;
|
||||
my $ping = Net::Ping->new($pingtype,$timeout,$bytes);
|
||||
|
||||
if ($ping->ping($host,2)) {
|
||||
$ok = 1;
|
||||
}
|
||||
return $ok;
|
||||
}
|
||||
|
||||
sub get_time {
|
||||
my %ts;
|
||||
my $time_stamp;
|
||||
($ts{"ss"},$ts{"mm"},$ts{"hh"},$ts{"dd"},$ts{"mon"},$ts{"yy"},$ts{"we"},$ts{"doy"},$ts{"st"})=localtime;
|
||||
if ($ts{"ss"} < 10) { $ts{"ss"} = "0" . $ts{"ss"}; }
|
||||
if ($ts{"mm"} < 10) { $ts{"mm"} = "0" . $ts{"mm"}; }
|
||||
if ($ts{"hh"} < 10) { $ts{"hh"} = "0" . $ts{"hh"}; }
|
||||
$time_stamp = "$ts{'hh'}" . ":" . "$ts{'mm'}" . ":" . "$ts{'ss'}";
|
||||
return $time_stamp;
|
||||
}
|
||||
|
||||
sub get_timestamp {
|
||||
my %ts;
|
||||
my $time_stamp;
|
||||
($ts{"ss"},$ts{"mm"},$ts{"hh"},$ts{"dd"},$ts{"mon"},$ts{"yy"},$ts{"we"},$ts{"doy"},$ts{"st"})=localtime;
|
||||
$time_stamp = $ts{'hh'} * 3600 + $ts{'mm'} * 60 + $ts{'ss'};
|
||||
return $time_stamp;
|
||||
}
|
||||
|
||||
sub get_date {
|
||||
my (%ts,$ret);
|
||||
($ts{"ss"},$ts{"mm"},$ts{"hh"},$ts{"dd"},$ts{"mon"},$ts{"yy"},$ts{"we"},$ts{"doy"},$ts{"st"})=localtime;
|
||||
$ts{"yy"} += 1900;
|
||||
$ts{"mon"} += 1;
|
||||
if ($ts{"dd"} < 10) { $ts{"dd"} = "0" . $ts{"dd"}; }
|
||||
if ($ts{"mon"} < 10) { $ts{"mon"} = "0" . $ts{"mon"}; }
|
||||
$ret = "$ts{'dd'}" . "-" . "$ts{'mon'}" . "-" . "$ts{'yy'}";
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub read_database {
|
||||
my ($sth,$ref,$sth1,$ref1,$sth2,$ref2);
|
||||
|
||||
foreach my $todo ( keys %{$c2->{'todo'}} ) { # durchläuft alle im speicher vorhanden todos
|
||||
$sth = $dbh->prepare("select ind from todos where t_active='1'"); # liest aktive todos aus db
|
||||
$sth->execute;
|
||||
# print "$todo:";
|
||||
while ($ref = $sth->fetchrow_arrayref()) { # durchläuft alle todos aus db
|
||||
my $found = 0;
|
||||
if ($ref->[0] == $todo) { # wenn todo aus speicher in db gefunden und in db auf 'aktiv'
|
||||
$found=1; # dann soll todo auch ausgeführt werden
|
||||
}
|
||||
unless ($found) { # wenn todo aus speicher nicht in db, oder in db 'inaktiv'
|
||||
delete $c2->{'todo'}->{$todo}; # dann todo aus speicher nehmen um check nicht durchzuführen
|
||||
}
|
||||
}
|
||||
}
|
||||
# 8
|
||||
|
||||
$sth = $dbh->prepare("select ind,t_host,t_service,t_check,t_alert from todos where t_active='1'");
|
||||
$sth->execute;
|
||||
while ($ref = $sth->fetchrow_arrayref()) { # aktive todos durchlaufen
|
||||
read_conf("$ref->[0]","hosts","$ref->[1]"); #Hosts lesen
|
||||
read_conf("$ref->[0]","alerts","$ref->[4]"); #Alerts lesen
|
||||
read_conf("$ref->[0]","checks","$ref->[3]"); #Checks lesen
|
||||
|
||||
# Services lesen
|
||||
$sth1 = $dbh->prepare("select position from services where service_ind = '$ref->[2]'");
|
||||
$sth1->execute;
|
||||
while ($ref1 = $sth1->fetchrow_arrayref()) { # Einzelne Felder der Tabellen durchlaufen
|
||||
$sth2 = $dbh->prepare("show fields from services");
|
||||
$sth2->execute;
|
||||
while ($ref2 = $sth2->fetchrow_arrayref()) {
|
||||
$c2->{"todo"}->{"$ref->[0]"}->{"services"}->{"$ref1->[0]"}->{"$ref2->[0]"} = get_vari("services", "$ref2->[0]", "service_ind = '$ref->[2]' and position = '$ref1->[0]'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Konfig lesen
|
||||
#$dbh1 = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
|
||||
$sth1 = $dbh->prepare("show fields from conf");
|
||||
$sth1->execute;
|
||||
while ($ref1 = $sth1->fetchrow_arrayref()) { # Einzelne Felder der Tabellen durchlaufen
|
||||
$c2->{"conf"}->{"$ref1->[0]"} = get_vari("conf", "$ref1->[0]", "active = '1'");
|
||||
}
|
||||
|
||||
# Error lesen
|
||||
#$dbh1 = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
|
||||
$sth1 = $dbh->prepare("select error_number from errors");
|
||||
$sth1->execute;
|
||||
while ($ref1 = $sth1->fetchrow_arrayref()) {
|
||||
#$error_nr = $ref1->[0];
|
||||
#$dbh2 = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
|
||||
$sth2 = $dbh->prepare("show fields from errors");
|
||||
$sth2->execute;
|
||||
while ($ref2 = $sth2->fetchrow_arrayref()) { # Einzelne Felder der Tabellen durchlaufen
|
||||
#$field = $ref2->[0];
|
||||
$c2->{"error"}->{"$ref1->[0]"}->{"$ref2->[0]"} = get_vari("errors", "$ref2->[0]", "error_number = '$ref1->[0]'");
|
||||
}
|
||||
}
|
||||
1;
|
||||
}
|
||||
|
||||
sub get_vari {
|
||||
my ($table,$variable,$bedingung) = @_;
|
||||
my $ret;
|
||||
my $sth = $dbh->prepare("select $variable from $table where $bedingung");
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
$ret = $ref->[0];
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub read_conf {
|
||||
my ($ind,$table,$value) = @_;
|
||||
my $sth = $dbh->prepare("show fields from $table");
|
||||
$sth->execute;
|
||||
while (my $ref = $sth->fetchrow_arrayref()) {
|
||||
$c2->{"todo"}->{"$ind"}->{"$table"}->{"$ref->[0]"} = get_vari("$table", "$ref->[0]", "ind = '$value'");
|
||||
}
|
||||
}
|
||||
|
||||
sub clear_line {
|
||||
# Entfernt Zeilenendezeichen
|
||||
my ($line) = @_;
|
||||
chomp $line;
|
||||
$line = delete_front_back_spaces ($line);
|
||||
return $line;
|
||||
}
|
||||
|
||||
sub delete_front_back_spaces {
|
||||
# Entfernt Leerzeichen am Zeilenanfang und Zeilenende
|
||||
my ($line) = @_;
|
||||
unless ($line eq "") {
|
||||
$line=~s/^ *//g;
|
||||
$line=~s/ *$//g;
|
||||
}
|
||||
return $line;
|
||||
}
|
||||
Reference in New Issue
Block a user