This commit is contained in:
2024-10-14 00:08:40 +02:00
parent dbfba56f66
commit 1462d52e13
4572 changed files with 2658864 additions and 0 deletions
+858
View File
@@ -0,0 +1,858 @@
### -*- mode: Perl -*-
######################################################################
### BER (Basic Encoding Rules) encoding and decoding.
######################################################################
### Copyright (c) 1995-2002, Simon Leinen.
###
### This program is free software; you can redistribute it under the
### "Artistic License" included in this distribution (file "Artistic").
######################################################################
### This module implements encoding and decoding of ASN.1-based data
### structures using the Basic Encoding Rules (BER). Only the subset
### necessary for SNMP is implemented.
######################################################################
### Created by: Simon Leinen <simon@switch.ch>
###
### Contributions and fixes by:
###
### Andrzej Tobola <san@iem.pw.edu.pl>: Added long String decode
### Tobias Oetiker <oetiker@ee.ethz.ch>: Added 5 Byte Integer decode ...
### Dave Rand <dlr@Bungi.com>: Added SysUpTime decode
### Philippe Simonet <sip00@vg.swissptt.ch>: Support larger subids
### Yufang HU <yhu@casc.com>: Support even larger subids
### Mike Mitchell <mcm@unx.sas.com>: New generalized encode_int()
### Mike Diehn <mdiehn@mindspring.net>: encode_ip_address()
### Rik Hoorelbeke <rik.hoorelbeke@pandora.be>: encode_oid() fix
### Brett T Warden <wardenb@eluminant.com>: pretty UInteger32
### Bert Driehuis <driehuis@playbeing.org>: Handle SNMPv2 exception codes
### Jakob Ilves (/IlvJa) <jakob.ilves@oracle.com>: PDU decoding
######################################################################
package BER;
require 5.002;
use strict;
use vars qw(@ISA @EXPORT $VERSION $pretty_print_timeticks $errmsg);
use Exporter;
$VERSION = '0.95';
@ISA = qw(Exporter);
@EXPORT = qw(context_flag constructor_flag
encode_int encode_int_0 encode_null encode_oid
encode_sequence encode_tagged_sequence
encode_string encode_ip_address encode_timeticks
encode_uinteger32 encode_counter32 encode_counter64
encode_gauge32
decode_sequence decode_by_template
pretty_print pretty_print_timeticks
hex_string hex_string_of_type
encoded_oid_prefix_p errmsg);
### Variables
## Bind this to zero if you want to avoid that TimeTicks are converted
## into "human readable" strings containing days, hours, minutes and
## seconds.
##
## If the variable is zero, pretty_print will simply return an
## unsigned integer representing hundredths of seconds.
##
$pretty_print_timeticks = 1;
### Prototypes
sub encode_header ($$);
sub encode_int_0 ();
sub encode_int ($);
sub encode_oid (@);
sub encode_null ();
sub encode_sequence (@);
sub encode_tagged_sequence ($@);
sub encode_string ($);
sub encode_ip_address ($);
sub encode_timeticks ($);
sub pretty_print ($);
sub pretty_using_decoder ($$);
sub pretty_string ($);
sub pretty_intlike ($);
sub pretty_unsignedlike ($);
sub pretty_oid ($);
sub pretty_uptime ($);
sub pretty_uptime_value ($);
sub pretty_ip_address ($);
sub pretty_generic_sequence ($);
sub hex_string ($);
sub hex_string_of_type ($$);
sub decode_oid ($);
sub decode_by_template;
sub decode_by_template_2;
sub decode_sequence ($);
sub decode_int ($);
sub decode_intlike ($);
sub decode_unsignedlike ($);
sub decode_intlike_s ($$);
sub decode_string ($);
sub decode_length ($);
sub encoded_oid_prefix_p ($$);
sub decode_subid ($$$);
sub decode_generic_tlv ($);
sub error (@);
sub template_error ($$$);
sub version () { $VERSION; }
### Flags for different types of tags
sub universal_flag { 0x00 }
sub application_flag { 0x40 }
sub context_flag { 0x80 }
sub private_flag { 0xc0 }
sub primitive_flag { 0x00 }
sub constructor_flag { 0x20 }
### Universal tags
sub boolean_tag { 0x01 }
sub int_tag { 0x02 }
sub bit_string_tag { 0x03 }
sub octet_string_tag { 0x04 }
sub null_tag { 0x05 }
sub object_id_tag { 0x06 }
sub sequence_tag { 0x10 }
sub set_tag { 0x11 }
sub uptime_tag { 0x43 }
### Flag for length octet announcing multi-byte length field
sub long_length { 0x80 }
### SNMP specific tags
sub snmp_ip_address_tag { 0x00 | application_flag }
sub snmp_counter32_tag { 0x01 | application_flag }
sub snmp_gauge32_tag { 0x02 | application_flag }
sub snmp_timeticks_tag { 0x03 | application_flag }
sub snmp_opaque_tag { 0x04 | application_flag }
sub snmp_nsap_address_tag { 0x05 | application_flag }
sub snmp_counter64_tag { 0x06 | application_flag }
sub snmp_uinteger32_tag { 0x07 | application_flag }
## Error codes (SNMPv2 and later)
##
sub snmp_nosuchobject { context_flag | 0x00 }
sub snmp_nosuchinstance { context_flag | 0x01 }
sub snmp_endofmibview { context_flag | 0x02 }
#### Encoding
sub encode_header ($$) {
my ($type,$length) = @_;
return pack ("C C", $type, $length) if $length < 128;
return pack ("C C C", $type, long_length | 1, $length) if $length < 256;
return pack ("C C n", $type, long_length | 2, $length) if $length < 65536;
return error ("Cannot encode length $length yet");
}
sub encode_int_0 () {
return pack ("C C C", 2, 1, 0);
}
sub encode_int ($) {
return encode_intlike ($_[0], int_tag);
}
sub encode_uinteger32 ($) {
return encode_intlike ($_[0], snmp_uinteger32_tag);
}
sub encode_counter32 ($) {
return encode_intlike ($_[0], snmp_counter32_tag);
}
sub encode_counter64 ($) {
return encode_intlike ($_[0], snmp_counter64_tag);
}
sub encode_gauge32 ($) {
return encode_intlike ($_[0], snmp_gauge32_tag);
}
sub encode_intlike ($$) {
my ($int, $tag)=@_;
my ($sign, $val, @vals);
$sign = ($int >= 0) ? 0 : 0xff;
if (ref $int && $int->isa ("Math::BigInt")) {
for(;;) {
$val = $int->bmod (256);
unshift(@vals, $val);
return encode_header ($tag, $#vals + 1).pack ("C*", @vals)
if ($int >= -128 && $int < 128);
$int = $int - $sign;
$int = $int / 256;
}
} else {
for(;;) {
$val = $int & 0xff;
unshift(@vals, $val);
return encode_header ($tag, $#vals + 1).pack ("C*", @vals)
if ($int >= -128 && $int < 128);
$int -= $sign;
$int = int($int / 256);
}
}
}
sub encode_oid (@) {
my @oid = @_;
my ($result,$subid);
$result = '';
## Ignore leading empty sub-ID. The favourite reason for
## those to occur is that people cut&paste numeric OIDs from
## CMU/UCD SNMP including the leading dot.
shift @oid if $oid[0] eq '';
return error ("Object ID too short: ", join('.',@oid))
if $#oid < 1;
## The first two subids in an Object ID are encoded as a single
## byte in BER, according to a funny convention. This poses
## restrictions on the ranges of those subids. In the past, I
## didn't check for those. But since so many people try to use
## OIDs in CMU/UCD SNMP's format and leave out the mib-2 or
## enterprises prefix, I introduced this check to catch those
## errors.
##
return error ("first subid too big in Object ID ", join('.',@oid))
if $oid[0] > 2;
$result = shift (@oid) * 40;
$result += shift @oid;
return error ("second subid too big in Object ID ", join('.',@oid))
if $result > 255;
$result = pack ("C", $result);
foreach $subid (@oid) {
if ( ($subid>=0) && ($subid<128) ){ #7 bits long subid
$result .= pack ("C", $subid);
} elsif ( ($subid>=128) && ($subid<16384) ){ #14 bits long subid
$result .= pack ("CC", 0x80 | $subid >> 7, $subid & 0x7f);
}
elsif ( ($subid>=16384) && ($subid<2097152) ) {#21 bits long subid
$result .= pack ("CCC",
0x80 | (($subid>>14) & 0x7f),
0x80 | (($subid>>7) & 0x7f),
$subid & 0x7f);
} elsif ( ($subid>=2097152) && ($subid<268435456) ){ #28 bits long subid
$result .= pack ("CCCC",
0x80 | (($subid>>21) & 0x7f),
0x80 | (($subid>>14) & 0x7f),
0x80 | (($subid>>7) & 0x7f),
$subid & 0x7f);
} elsif ( ($subid>=268435456) && ($subid<4294967296) ){ #32 bits long subid
$result .= pack ("CCCCC",
0x80 | (($subid>>28) & 0x0f), #mask the bits beyond 32
0x80 | (($subid>>21) & 0x7f),
0x80 | (($subid>>14) & 0x7f),
0x80 | (($subid>>7) & 0x7f),
$subid & 0x7f);
} else {
return error ("Cannot encode subid $subid");
}
}
encode_header (object_id_tag, length $result).$result;
}
sub encode_null () { encode_header (null_tag, 0); }
sub encode_sequence (@) { encode_tagged_sequence (sequence_tag, @_); }
sub encode_tagged_sequence ($@) {
my ($tag,$result);
$tag = shift @_;
$result = join '',@_;
return encode_header ($tag | constructor_flag, length $result).$result;
}
sub encode_string ($) {
my ($string)=@_;
return encode_header (octet_string_tag, length $string).$string;
}
sub encode_ip_address ($) {
my ($addr)=@_;
my @octets;
if (length $addr == 4) {
## Four bytes... let's suppose that this is a binary IP address
## in network byte order.
return encode_header (snmp_ip_address_tag, length $addr).$addr;
} elsif (@octets = ($addr =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
return encode_ip_address (pack ("CCCC", @octets));
} else {
return error ("IP address must be four bytes long or a dotted-quad");
}
}
sub encode_timeticks ($) {
my ($tt) = @_;
return encode_intlike ($tt, snmp_timeticks_tag);
}
#### Decoding
sub pretty_print ($) {
my ($packet) = @_;
my ($type,$rest);
return undef unless defined $packet;
my $result = ord (substr ($packet, 0, 1));
return pretty_intlike ($packet)
if $result == int_tag;
return pretty_unsignedlike ($packet)
if $result == snmp_counter32_tag
|| $result == snmp_gauge32_tag
|| $result == snmp_counter64_tag
|| $result == snmp_uinteger32_tag;
return pretty_string ($packet) if $result == octet_string_tag;
return pretty_oid ($packet) if $result == object_id_tag;
return ($pretty_print_timeticks
? pretty_uptime ($packet)
: pretty_unsignedlike ($packet))
if $result == uptime_tag;
return pretty_ip_address ($packet) if $result == snmp_ip_address_tag;
return "(null)" if $result == null_tag;
return error ("Exception code: noSuchObject") if $result == snmp_nosuchobject;
return error ("Exception code: noSuchInstance") if $result == snmp_nosuchinstance;
return error ("Exception code: endOfMibView") if $result == snmp_endofmibview;
# IlvJa
# pretty print sequences and their contents.
my $ctx_cons_flags = context_flag | constructor_flag;
if($result == (&constructor_flag | &sequence_tag) # sequence
|| $result == (0 | $ctx_cons_flags) #get_request
|| $result == (1 | $ctx_cons_flags) #getnext_request
|| $result == (2 | $ctx_cons_flags) #get_response
|| $result == (3 | $ctx_cons_flags) #set_request
|| $result == (4 | $ctx_cons_flags) #trap_request
|| $result == (5 | $ctx_cons_flags) #getbulk_request
|| $result == (6 | $ctx_cons_flags) #inform_request
|| $result == (7 | $ctx_cons_flags) #trap2_request
)
{
my $pretty_result = pretty_generic_sequence($packet);
$pretty_result =~ s/^/ /gm; #Indent.
my $seq_type_desc =
{
(constructor_flag | sequence_tag) => "Sequence",
(0 | $ctx_cons_flags) => "GetRequest",
(1 | $ctx_cons_flags) => "GetNextRequest",
(2 | $ctx_cons_flags) => "GetResponse",
(3 | $ctx_cons_flags) => "SetRequest",
(4 | $ctx_cons_flags) => "TrapRequest",
(5 | $ctx_cons_flags) => "GetbulkRequest",
(6 | $ctx_cons_flags) => "InformRequest",
(7 | $ctx_cons_flags) => "Trap2Request",
}->{($result)};
return $seq_type_desc . "{\n" . $pretty_result . "\n}";
}
return sprintf ("#<unprintable BER type 0x%x>", $result);
}
sub pretty_using_decoder ($$) {
my ($decoder, $packet) = @_;
my ($decoded,$rest);
($decoded,$rest) = &$decoder ($packet);
return error ("Junk after object") unless $rest eq '';
return $decoded;
}
sub pretty_string ($) {
pretty_using_decoder (\&decode_string, $_[0]);
}
sub pretty_intlike ($) {
my $decoded = pretty_using_decoder (\&decode_intlike, $_[0]);
$decoded;
}
sub pretty_unsignedlike ($) {
return pretty_using_decoder (\&decode_unsignedlike, $_[0]);
}
sub pretty_oid ($) {
my ($oid) = shift;
my ($result,$subid,$next);
my (@oid);
$result = ord (substr ($oid, 0, 1));
return error ("Object ID expected") unless $result == object_id_tag;
($result, $oid) = decode_length (substr ($oid, 1));
return error ("inconsistent length in OID") unless $result == length $oid;
@oid = ();
$subid = ord (substr ($oid, 0, 1));
push @oid, int ($subid / 40);
push @oid, $subid % 40;
$oid = substr ($oid, 1);
while ($oid ne '') {
$subid = ord (substr ($oid, 0, 1));
if ($subid < 128) {
$oid = substr ($oid, 1);
push @oid, $subid;
} else {
$next = $subid;
$subid = 0;
while ($next >= 128) {
$subid = ($subid << 7) + ($next & 0x7f);
$oid = substr ($oid, 1);
$next = ord (substr ($oid, 0, 1));
}
$subid = ($subid << 7) + $next;
$oid = substr ($oid, 1);
push @oid, $subid;
}
}
join ('.', @oid);
}
sub pretty_uptime ($) {
my ($packet,$uptime);
($uptime,$packet) = &decode_unsignedlike (@_);
pretty_uptime_value ($uptime);
}
sub pretty_uptime_value ($) {
my ($uptime) = @_;
my ($seconds,$minutes,$hours,$days,$result);
## We divide the uptime by hundred since we're not interested in
## sub-second precision.
$uptime = int ($uptime / 100);
$days = int ($uptime / (60 * 60 * 24));
$uptime %= (60 * 60 * 24);
$hours = int ($uptime / (60 * 60));
$uptime %= (60 * 60);
$minutes = int ($uptime / 60);
$seconds = $uptime % 60;
if ($days == 0){
$result = sprintf ("%d:%02d:%02d", $hours, $minutes, $seconds);
} elsif ($days == 1) {
$result = sprintf ("%d day, %d:%02d:%02d",
$days, $hours, $minutes, $seconds);
} else {
$result = sprintf ("%d days, %d:%02d:%02d",
$days, $hours, $minutes, $seconds);
}
return $result;
}
sub pretty_ip_address ($) {
my $pdu = shift;
my ($length, $rest);
return error ("IP Address tag (".snmp_ip_address_tag.") expected")
unless ord (substr ($pdu, 0, 1)) == snmp_ip_address_tag;
$pdu = substr ($pdu, 1);
($length,$pdu) = decode_length ($pdu);
return error ("Length of IP address should be four")
unless $length == 4;
sprintf "%d.%d.%d.%d", unpack ("CCCC", $pdu);
}
# IlvJa
# Returns a string with the pretty prints of all
# the elements in the sequence.
sub pretty_generic_sequence ($) {
my ($pdu) = shift;
my $rest;
my $type = ord substr ($pdu, 0 ,1);
my $flags = context_flag | constructor_flag;
return error (sprintf ("Tag 0x%x is not a valid sequence tag",$type))
unless ($type == (&constructor_flag | &sequence_tag) # sequence
|| $type == (0 | $flags) #get_request
|| $type == (1 | $flags) #getnext_request
|| $type == (2 | $flags) #get_response
|| $type == (3 | $flags) #set_request
|| $type == (4 | $flags) #trap_request
|| $type == (5 | $flags) #getbulk_request
|| $type == (6 | $flags) #inform_request
|| $type == (7 | $flags) #trap2_request
);
my $curelem;
my $pretty_result; # Holds the pretty printed sequence.
my $pretty_elem; # Holds the pretty printed current elem.
my $first_elem = 'true';
# Cut away the first Tag and Length from $packet and then
# init $rest with that.
(undef, $rest) = decode_length(substr $pdu, 1);
while($rest)
{
($curelem,$rest) = decode_generic_tlv($rest);
$pretty_elem = pretty_print($curelem);
$pretty_result .= "\n" if not $first_elem;
$pretty_result .= $pretty_elem;
# The rest of the iterations are not related to the
# first element of the sequence so..
$first_elem = '' if $first_elem;
}
return $pretty_result;
}
sub hex_string ($) {
&hex_string_of_type ($_[0], octet_string_tag);
}
sub hex_string_of_type ($$) {
my ($pdu, $wanted_type) = @_;
my ($length);
return error ("BER tag ".$wanted_type." expected")
unless ord (substr ($pdu, 0, 1)) == $wanted_type;
$pdu = substr ($pdu, 1);
($length,$pdu) = decode_length ($pdu);
hex_string_aux ($pdu);
}
sub hex_string_aux ($) {
my ($binary_string) = @_;
my ($c, $result);
$result = '';
for $c (unpack "C*", $binary_string) {
$result .= sprintf "%02x", $c;
}
$result;
}
sub decode_oid ($) {
my ($pdu) = @_;
my ($result,$pdu_rest);
my (@result);
$result = ord (substr ($pdu, 0, 1));
return error ("Object ID expected") unless $result == object_id_tag;
($result, $pdu_rest) = decode_length (substr ($pdu, 1));
return error ("Short PDU")
if $result > length $pdu_rest;
@result = (substr ($pdu, 0, $result + (length ($pdu) - length ($pdu_rest))),
substr ($pdu_rest, $result));
@result;
}
# IlvJa
# This takes a PDU and returns a two element list consisting of
# the first element found in the PDU (whatever it is) and the
# rest of the PDU
sub decode_generic_tlv ($) {
my ($pdu) = @_;
my (@result);
my ($elemlength,$pdu_rest) = decode_length (substr($pdu,1));
@result = (# Extract the first element.
substr ($pdu, 0, $elemlength + (length ($pdu)
- length ($pdu_rest)
)
),
#Extract the rest of the PDU.
substr ($pdu_rest, $elemlength)
);
@result;
}
sub decode_by_template {
my ($pdu) = shift;
local ($_) = shift;
return decode_by_template_2 ($pdu, $_, 0, 0, @_);
}
my $template_debug = 0;
sub decode_by_template_2 {
my ($pdu, $template, $pdu_index, $template_index);
local ($_);
$pdu = shift;
$template = $_ = shift;
$pdu_index = shift;
$template_index = shift;
my (@results);
my ($length,$expected,$read,$rest);
return undef unless defined $pdu;
while (0 < length ($_)) {
if (substr ($_, 0, 1) eq '%') {
print STDERR "template $_ ", length $pdu," bytes remaining\n"
if $template_debug;
$_ = substr ($_,1);
++$template_index;
if (($expected) = /^(\d*|\*)\{(.*)/) {
## %{
$template_index += length ($expected) + 1;
print STDERR "%{\n" if $template_debug;
$_ = $2;
$expected = shift | constructor_flag if ($expected eq '*');
$expected = sequence_tag | constructor_flag
if $expected eq '';
return template_error ("Unexpected end of PDU",
$template, $template_index)
if !defined $pdu or $pdu eq '';
return template_error ("Expected sequence tag $expected, got ".
ord (substr ($pdu, 0, 1)),
$template,
$template_index)
unless (ord (substr ($pdu, 0, 1)) == $expected);
$pdu = substr ($pdu,1);
(($length,$pdu) = decode_length ($pdu))
|| return template_error ("cannot read length",
$template, $template_index);
return template_error ("Expected length $length, got ".length $pdu ,
$template, $template_index)
unless length $pdu == $length;
} elsif (($expected,$rest) = /^(\*|)s(.*)/) {
## %s
$template_index += length ($expected) + 1;
($expected = shift) if $expected eq '*';
(($read,$pdu) = decode_string ($pdu))
|| return template_error ("cannot read string",
$template, $template_index);
print STDERR "%s => $read\n" if $template_debug;
if ($expected eq '') {
push @results, $read;
} else {
return template_error ("Expected $expected, read $read",
$template, $template_index)
unless $expected eq $read;
}
$_ = $rest;
} elsif (($rest) = /^A(.*)/) {
## %A
$template_index += 1;
{
my ($tag, $length, $value);
$tag = ord (substr ($pdu, 0, 1));
return error ("Expected IP address, got tag ".$tag)
unless $tag == snmp_ip_address_tag;
($length, $pdu) = decode_length (substr ($pdu, 1));
return error ("Inconsistent length of InetAddress encoding")
if $length > length $pdu;
return template_error ("IP address must be four bytes long",
$template, $template_index)
unless $length == 4;
$read = substr ($pdu, 0, $length);
$pdu = substr ($pdu, $length);
}
print STDERR "%A => $read\n" if $template_debug;
push @results, $read;
$_ = $rest;
} elsif (/^O(.*)/) {
## %O
$template_index += 1;
$_ = $1;
(($read,$pdu) = decode_oid ($pdu))
|| return template_error ("cannot read OID",
$template, $template_index);
print STDERR "%O => ".pretty_oid ($read)."\n"
if $template_debug;
push @results, $read;
} elsif (($expected,$rest) = /^(\d*|\*|)i(.*)/) {
## %i
$template_index += length ($expected) + 1;
print STDERR "%i\n" if $template_debug;
$_ = $rest;
(($read,$pdu) = decode_int ($pdu))
|| return template_error ("cannot read int",
$template, $template_index);
if ($expected eq '') {
push @results, $read;
} else {
$expected = int (shift) if $expected eq '*';
return template_error (sprintf ("Expected %d (0x%x), got %d (0x%x)",
$expected, $expected, $read, $read),
$template, $template_index)
unless ($expected == $read)
}
} elsif (($rest) = /^u(.*)/) {
## %u
$template_index += 1;
print STDERR "%u\n" if $template_debug;
$_ = $rest;
(($read,$pdu) = decode_unsignedlike ($pdu))
|| return template_error ("cannot read uptime",
$template, $template_index);
push @results, $read;
} elsif (/^\@(.*)/) {
## %@
$template_index += 1;
print STDERR "%@\n" if $template_debug;
$_ = $1;
push @results, $pdu;
$pdu = '';
} else {
return template_error ("Unknown decoding directive in template: $_",
$template, $template_index);
}
} else {
if (substr ($_, 0, 1) ne substr ($pdu, 0, 1)) {
return template_error ("Expected ".substr ($_, 0, 1).", got ".substr ($pdu, 0, 1),
$template, $template_index);
}
$_ = substr ($_,1);
$pdu = substr ($pdu,1);
}
}
return template_error ("PDU too long", $template, $template_index)
if length ($pdu) > 0;
return template_error ("PDU too short", $template, $template_index)
if length ($_) > 0;
@results;
}
sub decode_sequence ($) {
my ($pdu) = @_;
my ($result);
my (@result);
$result = ord (substr ($pdu, 0, 1));
return error ("Sequence expected")
unless $result == (sequence_tag | constructor_flag);
($result, $pdu) = decode_length (substr ($pdu, 1));
return error ("Short PDU")
if $result > length $pdu;
@result = (substr ($pdu, 0, $result), substr ($pdu, $result));
@result;
}
sub decode_int ($) {
my ($pdu) = @_;
my $tag = ord (substr ($pdu, 0, 1));
return error ("Integer expected, found tag ".$tag)
unless $tag == int_tag;
decode_intlike ($pdu);
}
sub decode_intlike ($) {
decode_intlike_s ($_[0], 1);
}
sub decode_unsignedlike ($) {
decode_intlike_s ($_[0], 0);
}
my $have_math_bigint_p = 0;
sub decode_intlike_s ($$) {
my ($pdu, $signedp) = @_;
my ($length,$result);
$length = ord (substr ($pdu, 1, 1));
my $ptr = 2;
$result = unpack ($signedp ? "c" : "C", substr ($pdu, $ptr++, 1));
if ($length > 5 || ($length == 5 && $result > 0)) {
require 'Math/BigInt.pm' unless $have_math_bigint_p++;
$result = new Math::BigInt ($result);
}
while (--$length > 0) {
$result *= 256;
$result += unpack ("C", substr ($pdu, $ptr++, 1));
}
($result, substr ($pdu, $ptr));
}
sub decode_string ($) {
my ($pdu) = shift;
my ($result);
$result = ord (substr ($pdu, 0, 1));
return error ("Expected octet string, got tag ".$result)
unless $result == octet_string_tag;
($result, $pdu) = decode_length (substr ($pdu, 1));
return error ("Short PDU")
if $result > length $pdu;
return (substr ($pdu, 0, $result), substr ($pdu, $result));
}
sub decode_length ($) {
my ($pdu) = shift;
my ($result);
my (@result);
$result = ord (substr ($pdu, 0, 1));
if ($result & long_length) {
if ($result == (long_length | 1)) {
@result = (ord (substr ($pdu, 1, 1)), substr ($pdu, 2));
} elsif ($result == (long_length | 2)) {
@result = ((ord (substr ($pdu, 1, 1)) << 8)
+ ord (substr ($pdu, 2, 1)), substr ($pdu, 3));
} else {
return error ("Unsupported length");
}
} else {
@result = ($result, substr ($pdu, 1));
}
@result;
}
#### OID prefix check
### encoded_oid_prefix_p OID1 OID2
###
### OID1 and OID2 should be BER-encoded OIDs.
### The function returns non-zero iff OID1 is a prefix of OID2.
### This can be used in the termination condition of a loop that walks
### a table using GetNext or GetBulk.
###
sub encoded_oid_prefix_p ($$) {
my ($oid1, $oid2) = @_;
my ($i1, $i2);
my ($l1, $l2);
my ($subid1, $subid2);
return error ("OID tag expected") unless ord (substr ($oid1, 0, 1)) == object_id_tag;
return error ("OID tag expected") unless ord (substr ($oid2, 0, 1)) == object_id_tag;
($l1,$oid1) = decode_length (substr ($oid1, 1));
($l2,$oid2) = decode_length (substr ($oid2, 1));
for ($i1 = 0, $i2 = 0;
$i1 < $l1 && $i2 < $l2;
++$i1, ++$i2) {
($subid1,$i1) = &decode_subid ($oid1, $i1, $l1);
($subid2,$i2) = &decode_subid ($oid2, $i2, $l2);
return 0 unless $subid1 == $subid2;
}
return $i2 if $i1 == $l1;
return 0;
}
### decode_subid OID INDEX
###
### Decodes a subid field from a BER-encoded object ID.
### Returns two values: the field, and the index of the last byte that
### was actually decoded.
###
sub decode_subid ($$$) {
my ($oid, $i, $l) = @_;
my $subid = 0;
my $next;
while (($next = ord (substr ($oid, $i, 1))) >= 128) {
$subid = ($subid << 7) + ($next & 0x7f);
++$i;
return error ("decoding object ID: short field")
unless $i < $l;
}
return (($subid << 7) + $next, $i);
}
sub error (@) {
$errmsg = join ("",@_);
return undef;
}
sub template_error ($$$) {
my ($errmsg, $template, $index) = @_;
return error ($errmsg."\n ".$template."\n ".(' ' x $index)."^");
}
1;
+26
View File
@@ -0,0 +1,26 @@
<html>
<head>
[C2METAREFRESH]
<style type='text/css'>
select { font-size:8pt }
td { font-size:10pt }
</style>
<title>c2_mysql</title>
</head>
<body name='seite'>
<a href="[C2CONFIGURL]" target='konfig'>Konfiguration & Administration</a>
<table border="1">
<tr>
<td align="center" width="80"><b>Zeit</b></td>
<td align="center" width="100"><b>Host</b></td>
<td align="center" width="130"><b>IP</b></td>
<td align="center" width="150"><b>Dienst</b></td>
<td align="center" width="120"><b>Ergebnis</b></td>
<td align="center" width="100"><b>Check</b></td>
<td align="center" width="70"><b>Fehler</b></td>
<td align="center" width="100"><b>Alarm</b></td>
</tr>
[C2REPEAT] DisplayError <tr><td align="center">[C2SERVICETIME]</td><td align="center">[C2HOSTNAME]</td><td align="center">[C2HOSTIP]</td><td align="center">[C2SERVICENAME]</td><td align="center">[C2RESULT]</td><td align="center">[C2CHECKDESCRIPTION]</td><td align="center">[C2ERROR]</td><td align="center">[C2ALERTDESCRIPTION]</td></tr>
</table>
</body>
</html>
+972
View File
@@ -0,0 +1,972 @@
# -*- mode: Perl -*-
######################################################################
### SNMP Request/Response Handling
######################################################################
### Copyright (c) 1995-2002, Simon Leinen.
###
### This program is free software; you can redistribute it under the
### "Artistic License" included in this distribution (file "Artistic").
######################################################################
### The abstract class SNMP_Session defines objects that can be used
### to communicate with SNMP entities. It has methods to send
### requests to and receive responses from an agent.
###
### Two instantiable subclasses are defined:
### SNMPv1_Session implements SNMPv1 (RFC 1157) functionality
### SNMPv2c_Session implements community-based SNMPv2.
######################################################################
### Created by: Simon Leinen <simon@switch.ch>
###
### Contributions and fixes by:
###
### Matthew Trunnell <matter@media.mit.edu>
### Tobias Oetiker <oetiker@ee.ethz.ch>
### Heine Peters <peters@dkrz.de>
### Daniel L. Needles <dan_needles@INS.COM>
### Mike Mitchell <mcm@unx.sas.com>
### Clinton Wong <clintdw@netcom.com>
### Alan Nichols <Alan.Nichols@Ebay.Sun.COM>
### Mike McCauley <mikem@open.com.au>
### Andrew W. Elble <elble@icculus.nsg.nwu.edu>
### Brett T Warden <wardenb@eluminant.com>: pretty UInteger32
### Michael Deegan <michael@cnspc18.murdoch.edu.au>
### Sergio Macedo <macedo@tmp.com.br>
### Jakob Ilves (/IlvJa) <jakob.ilves@oracle.com>: PDU capture
######################################################################
package SNMP_Session;
require 5.002;
use strict;
use Exporter;
use vars qw(@ISA $VERSION @EXPORT $errmsg $suppress_warnings);
use Socket;
use BER;
use Carp;
sub map_table ($$$ );
sub map_table_4 ($$$$);
sub map_table_start_end ($$$$$$);
sub index_compare ($$);
sub oid_diff ($$);
$VERSION = '0.95';
@ISA = qw(Exporter);
@EXPORT = qw(errmsg suppress_warnings index_compare oid_diff recycle_socket);
my $default_debug = 0;
### Default initial timeout (in seconds) waiting for a response PDU
### after a request is sent. Note that when a request is retried, the
### timeout is increased by BACKOFF (see below).
###
my $default_timeout = 2.0;
### Default number of attempts to get a reply for an SNMP request. If
### no response is received after TIMEOUT seconds, the request is
### resent and a new response awaited with a longer timeout (see the
### documentation on BACKOFF below). The "retries" value should be at
### least 1, because the first attempt counts, too (the name "retries"
### is confusing, sorry for that).
###
my $default_retries = 5;
### Default backoff factor for SNMP_Session objects. This factor is
### used to increase the TIMEOUT every time an SNMP request is
### retried.
###
my $default_backoff = 1.0;
### Default value for maxRepetitions. This specifies how many table
### rows are requested in getBulk requests. Used when walking tables
### using getBulk (only available in SNMPv2(c) and later). If this is
### too small, then a table walk will need unnecessarily many
### request/response exchanges. If it is too big, the agent may
### compute many variables after the end of the table. It is
### recommended to set this explicitly for each table walk by using
### map_table_4().
###
my $default_max_repetitions = 12;
### Default value for "avoid_negative_request_ids".
###
### Set this to non-zero if you have agents that have trouble with
### negative request IDs, and don't forget to complain to your agent
### vendor. According to the spec (RFC 1905), the request-id is an
### Integer32, i.e. its range is from -(2^31) to (2^31)-1. However,
### some agents erroneously encode the response ID as an unsigned,
### which prevents this code from matching such responses to requests.
###
my $default_avoid_negative_request_ids = 0;
### Whether all SNMP_Session objects should share a single UDP socket.
###
$SNMP_Session::recycle_socket = 0;
my $the_socket;
$SNMP_Session::errmsg = '';
$SNMP_Session::suppress_warnings = 0;
sub get_request { 0 | context_flag };
sub getnext_request { 1 | context_flag };
sub get_response { 2 | context_flag };
sub set_request { 3 | context_flag };
sub trap_request { 4 | context_flag };
sub getbulk_request { 5 | context_flag };
sub inform_request { 6 | context_flag };
sub trap2_request { 7 | context_flag };
sub standard_udp_port { 161 };
sub open
{
return SNMPv1_Session::open (@_);
}
sub timeout { $_[0]->{timeout} }
sub retries { $_[0]->{retries} }
sub backoff { $_[0]->{backoff} }
sub set_timeout {
my ($session, $timeout) = @_;
croak ("timeout ($timeout) must be a positive number") unless $timeout > 0.0;
$session->{'timeout'} = $timeout;
}
sub set_retries {
my ($session, $retries) = @_;
croak ("retries ($retries) must be a non-negative integer")
unless $retries == int ($retries) && $retries >= 0;
$session->{'retries'} = $retries;
}
sub set_backoff {
my ($session, $backoff) = @_;
croak ("backoff ($backoff) must be a number >= 1.0")
unless $backoff == int ($backoff) && $backoff >= 1.0;
$session->{'backoff'} = $backoff;
}
sub encode_request_3 ($$$@) {
my($this, $reqtype, $encoded_oids_or_pairs, $i1, $i2) = @_;
my($request);
local($_);
$this->{request_id} = ($this->{request_id} == 0x7fffffff)
? ($this->{avoid_negative_request_ids}
? 0x00000000
: -0x80000000)
: $this->{request_id}+1;
foreach $_ (@{$encoded_oids_or_pairs}) {
if (ref ($_) eq 'ARRAY') {
$_ = &encode_sequence ($_->[0], $_->[1])
|| return $this->ber_error ("encoding pair");
} else {
$_ = &encode_sequence ($_, encode_null())
|| return $this->ber_error ("encoding value/null pair");
}
}
$request = encode_tagged_sequence
($reqtype,
encode_int ($this->{request_id}),
defined $i1 ? encode_int ($i1) : encode_int_0,
defined $i2 ? encode_int ($i2) : encode_int_0,
encode_sequence (@{$encoded_oids_or_pairs}))
|| return $this->ber_error ("encoding request PDU");
return $this->wrap_request ($request);
}
sub encode_get_request {
my($this, @oids) = @_;
return encode_request_3 ($this, get_request, \@oids);
}
sub encode_getnext_request {
my($this, @oids) = @_;
return encode_request_3 ($this, getnext_request, \@oids);
}
sub encode_getbulk_request {
my($this, $non_repeaters, $max_repetitions, @oids) = @_;
return encode_request_3 ($this, getbulk_request, \@oids,
$non_repeaters, $max_repetitions);
}
sub encode_set_request {
my($this, @encoded_pairs) = @_;
return encode_request_3 ($this, set_request, \@encoded_pairs);
}
sub encode_trap_request ($$$$$$@) {
my($this, $ent, $agent, $gen, $spec, $dt, @pairs) = @_;
my($request);
local($_);
foreach $_ (@pairs) {
if (ref ($_) eq 'ARRAY') {
$_ = &encode_sequence ($_->[0], $_->[1])
|| return $this->ber_error ("encoding pair");
} else {
$_ = &encode_sequence ($_, encode_null())
|| return $this->ber_error ("encoding value/null pair");
}
}
$request = encode_tagged_sequence
(trap_request, $ent, $agent, $gen, $spec, $dt, encode_sequence (@pairs))
|| return $this->ber_error ("encoding trap PDU");
return $this->wrap_request ($request);
}
sub encode_v2_trap_request ($@) {
my($this, @pairs) = @_;
return encode_request_3($this, trap2_request, \@pairs);
}
sub decode_get_response {
my($this, $response) = @_;
my @rest;
@{$this->{'unwrapped'}};
}
sub decode_trap_request ($$) {
my ($this, $trap) = @_;
my ($snmp_version, $community, $ent, $agent, $gen, $spec, $dt,
$request_id, $error_status, $error_index,
$bindings);
($snmp_version, $community,
$ent, $agent,
$gen, $spec, $dt,
$bindings)
= decode_by_template ($trap, "%{%i%s%*{%O%A%i%i%u%{%@",
trap_request);
if (! defined ($snmp_version)) {
($snmp_version, $community,
$request_id, $error_status, $error_index,
$bindings)
= decode_by_template ($trap, "%{%i%s%*{%i%i%i%{%@",
trap2_request);
return $this->error_return ("v2 trap request contained errorStatus/errorIndex "
.$error_status."/".$error_index)
if defined $error_status && defined $error_index
&& ($error_status != 0 || $error_index != 0);
}
if (!defined $snmp_version) {
return $this->error_return ("BER error decoding trap:\n ".$BER::errmsg);
}
return ($community, $ent, $agent, $gen, $spec, $dt, $bindings);
}
sub wait_for_response {
my($this) = shift;
my($timeout) = shift || 10.0;
my($rin,$win,$ein) = ('','','');
my($rout,$wout,$eout);
vec($rin,$this->sockfileno,1) = 1;
select($rout=$rin,$wout=$win,$eout=$ein,$timeout);
}
sub get_request_response ($@) {
my($this, @oids) = @_;
return $this->request_response_5 ($this->encode_get_request (@oids),
get_response, \@oids, 1);
}
sub set_request_response ($@) {
my($this, @pairs) = @_;
return $this->request_response_5 ($this->encode_set_request (@pairs),
get_response, \@pairs, 1);
}
sub getnext_request_response ($@) {
my($this,@oids) = @_;
return $this->request_response_5 ($this->encode_getnext_request (@oids),
get_response, \@oids, 1);
}
sub getbulk_request_response ($$$@) {
my($this,$non_repeaters,$max_repetitions,@oids) = @_;
return $this->request_response_5
($this->encode_getbulk_request ($non_repeaters,$max_repetitions,@oids),
get_response, \@oids, 1);
}
sub trap_request_send ($$$$$$@) {
my($this, $ent, $agent, $gen, $spec, $dt, @pairs) = @_;
my($req);
$req = $this->encode_trap_request ($ent, $agent, $gen, $spec, $dt, @pairs);
## Encoding may have returned an error.
return undef unless defined $req;
$this->send_query($req)
|| return $this->error ("send_trap: $!");
return 1;
}
sub v2_trap_request_send ($$$@) {
my($this, $trap_oid, $dt, @pairs) = @_;
my @sysUptime_OID = ( 1,3,6,1,2,1,1,3 );
my @snmpTrapOID_OID = ( 1,3,6,1,6,3,1,1,4,1 );
my($req);
unshift @pairs, [encode_oid (@snmpTrapOID_OID,0),
encode_oid (@{$trap_oid})];
unshift @pairs, [encode_oid (@sysUptime_OID,0),
encode_timeticks ($dt)];
$req = $this->encode_v2_trap_request (@pairs);
## Encoding may have returned an error.
return undef unless defined $req;
$this->send_query($req)
|| return $this->error ("send_trap: $!");
return 1;
}
sub request_response_5 ($$$$$) {
my ($this, $req, $response_tag, $oids, $errorp) = @_;
my $retries = $this->retries;
my $timeout = $this->timeout;
my ($nfound, $timeleft);
## Encoding may have returned an error.
return undef unless defined $req;
$timeleft = $timeout;
while ($retries > 0) {
$this->send_query ($req)
|| return $this->error ("send_query: $!");
# IlvJa
# Add request pdu to capture_buffer
push @{$this->{'capture_buffer'}}, $req
if (defined $this->{'capture_buffer'}
and ref $this->{'capture_buffer'} eq 'ARRAY');
#
wait_for_response:
($nfound, $timeleft) = $this->wait_for_response($timeleft);
if ($nfound > 0) {
my($response_length);
$response_length
= $this->receive_response_3 ($response_tag, $oids, $errorp);
if ($response_length) {
# IlvJa
# Add response pdu to capture_buffer
push (@{$this->{'capture_buffer'}},
substr($this->{'pdu_buffer'}, 0, $response_length)
)
if (defined $this->{'capture_buffer'}
and ref $this->{'capture_buffer'} eq 'ARRAY');
#
return $response_length;
} elsif (defined ($response_length)) {
goto wait_for_response;
# A response has been received, but for a different
# request ID or from a different IP address.
} else {
return undef;
}
} else {
## No response received - retry
--$retries;
$timeout *= $this->backoff;
$timeleft = $timeout;
}
}
# IlvJa
# Add empty packet to capture_buffer
push @{$this->{'capture_buffer'}}, ""
if (defined $this->{'capture_buffer'}
and ref $this->{'capture_buffer'} eq 'ARRAY');
#
$this->error ("no response received");
}
sub map_table ($$$) {
my ($session, $columns, $mapfn) = @_;
return $session->map_table_4 ($columns, $mapfn,
$session->default_max_repetitions ());
}
sub map_table_4 ($$$$) {
my ($session, $columns, $mapfn, $max_repetitions) = @_;
return $session->map_table_start_end ($columns, $mapfn,
"", undef,
$max_repetitions);
}
sub map_table_start_end ($$$$$$) {
my ($session, $columns, $mapfn, $start, $end, $max_repetitions) = @_;
my @encoded_oids;
my $call_counter = 0;
my $base_index = $start;
do {
foreach (@encoded_oids = @{$columns}) {
$_=encode_oid (@{$_},split '\.',$base_index)
|| return $session->ber_error ("encoding OID $base_index");
}
if ($session->getnext_request_response (@encoded_oids)) {
my $response = $session->pdu_buffer;
my ($bindings) = $session->decode_get_response ($response);
my $smallest_index = undef;
my @collected_values = ();
my @bases = @{$columns};
while ($bindings ne '') {
my ($binding, $oid, $value);
my $base = shift @bases;
($binding, $bindings) = decode_sequence ($bindings);
($oid, $value) = decode_by_template ($binding, "%O%@");
my $out_index;
$out_index = &oid_diff ($base, $oid);
my $cmp;
if (!defined $smallest_index
|| ($cmp = index_compare ($out_index,$smallest_index)) == -1) {
$smallest_index = $out_index;
grep ($_=undef, @collected_values);
push @collected_values, $value;
} elsif ($cmp == 1) {
push @collected_values, undef;
} else {
push @collected_values, $value;
}
}
(++$call_counter,
&$mapfn ($smallest_index, @collected_values))
if defined $smallest_index;
$base_index = $smallest_index;
} else {
return undef;
}
}
while (defined $base_index
&& (!defined $end || index_compare ($base_index, $end) < 0));
$call_counter;
}
sub index_compare ($$) {
my ($i1, $i2) = @_;
$i1 = '' unless defined $i1;
$i2 = '' unless defined $i2;
if ($i1 eq '') {
return $i2 eq '' ? 0 : 1;
} elsif ($i2 eq '') {
return 1;
} elsif (!$i1) {
return $i2 eq '' ? 1 : !$i2 ? 0 : 1;
} elsif (!$i2) {
return -1;
} else {
my ($f1,$r1) = split('\.',$i1,2);
my ($f2,$r2) = split('\.',$i2,2);
if ($f1 < $f2) {
return -1;
} elsif ($f1 > $f2) {
return 1;
} else {
return index_compare ($r1,$r2);
}
}
}
sub oid_diff ($$) {
my($base, $full) = @_;
my $base_dotnot = join ('.',@{$base});
my $full_dotnot = BER::pretty_oid ($full);
return undef unless substr ($full_dotnot, 0, length $base_dotnot)
eq $base_dotnot
&& substr ($full_dotnot, length $base_dotnot, 1) eq '.';
substr ($full_dotnot, length ($base_dotnot)+1);
}
sub pretty_address {
my($addr) = shift;
my($port,$ipaddr) = unpack_sockaddr_in($addr);
return sprintf ("[%s].%d",inet_ntoa($ipaddr),$port);
}
sub version { $VERSION; }
sub error_return ($$) {
my ($this,$message) = @_;
$SNMP_Session::errmsg = $message;
unless ($SNMP_Session::suppress_warnings) {
$message =~ s/^/ /mg;
carp ("Error:\n".$message."\n");
}
return undef;
}
sub error ($$) {
my ($this,$message) = @_;
my $session = $this->to_string;
$SNMP_Session::errmsg = $message."\n".$session;
unless ($SNMP_Session::suppress_warnings) {
$session =~ s/^/ /mg;
$message =~ s/^/ /mg;
carp ("SNMP Error:\n".$SNMP_Session::errmsg."\n");
}
return undef;
}
sub ber_error ($$) {
my ($this,$type) = @_;
my ($errmsg) = $BER::errmsg;
$errmsg =~ s/^/ /mg;
return $this->error ("$type:\n$errmsg");
}
package SNMPv1_Session;
use strict qw(vars subs); # see above
use vars qw(@ISA);
use SNMP_Session;
use Socket;
use BER;
use IO::Socket;
use Carp;
@ISA = qw(SNMP_Session);
sub snmp_version { 0 }
sub open {
my($this,
$remote_hostname,$community,$port,
$max_pdu_len,$local_port,$max_repetitions,
$local_hostname) = @_;
my($remote_addr,$socket);
$community = 'public' unless defined $community;
$port = SNMP_Session::standard_udp_port unless defined $port;
$max_pdu_len = 8000 unless defined $max_pdu_len;
$max_repetitions = $default_max_repetitions
unless defined $max_repetitions;
if (defined $remote_hostname) {
$remote_addr = inet_aton ($remote_hostname)
or return $this->error_return ("can't resolve \"$remote_hostname\" to IP address");
}
if ($SNMP_Session::recycle_socket && defined $the_socket) {
$socket = $the_socket;
} else {
$socket = IO::Socket::INET->new(Proto => 17,
Type => SOCK_DGRAM,
LocalAddr => $local_hostname,
LocalPort => $local_port)
|| return $this->error_return ("creating socket: $!");
$the_socket = $socket
if $SNMP_Session::recycle_socket;
}
$remote_addr = pack_sockaddr_in ($port, $remote_addr)
if defined $remote_addr;
bless {
'sock' => $socket,
'sockfileno' => fileno ($socket),
'community' => $community,
'remote_hostname' => $remote_hostname,
'remote_addr' => $remote_addr,
'max_pdu_len' => $max_pdu_len,
'pdu_buffer' => '\0' x $max_pdu_len,
'request_id' =>
$default_avoid_negative_request_ids
? (int (rand 0x8000) << 16) + int (rand 0x10000)
: (int (rand 0x10000) << 16) + int (rand 0x10000)
- 0x80000000,
'timeout' => $default_timeout,
'retries' => $default_retries,
'backoff' => $default_backoff,
'debug' => $default_debug,
'error_status' => 0,
'error_index' => 0,
'default_max_repetitions' => $max_repetitions,
'use_getbulk' => 1,
'lenient_source_address_matching' => 1,
'lenient_source_port_matching' => 1,
'avoid_negative_request_ids' => $default_avoid_negative_request_ids,
'capture_buffer' => undef,
};
}
sub open_trap_session (@) {
my ($this, $port) = @_;
$port = 162 unless defined $port;
return $this->open (undef, "", 161, undef, $port);
}
sub sock { $_[0]->{sock} }
sub sockfileno { $_[0]->{sockfileno} }
sub remote_addr { $_[0]->{remote_addr} }
sub pdu_buffer { $_[0]->{pdu_buffer} }
sub max_pdu_len { $_[0]->{max_pdu_len} }
sub default_max_repetitions {
defined $_[1]
? $_[0]->{default_max_repetitions} = $_[1]
: $_[0]->{default_max_repetitions} }
sub debug { defined $_[1] ? $_[0]->{debug} = $_[1] : $_[0]->{debug} }
sub close {
my($this) = shift;
## Avoid closing the socket if it may be shared with other session
## objects.
if (! defined $the_socket || $this->sock ne $the_socket) {
close ($this->sock) || $this->error ("close: $!");
}
}
sub wrap_request {
my($this) = shift;
my($request) = shift;
encode_sequence (encode_int ($this->snmp_version),
encode_string ($this->{community}),
$request)
|| return $this->ber_error ("wrapping up request PDU");
}
my @error_status_code = qw(noError tooBig noSuchName badValue readOnly
genErr noAccess wrongType wrongLength
wrongEncoding wrongValue noCreation
inconsistentValue resourceUnavailable
commitFailed undoFailed authorizationError
notWritable inconsistentName);
sub unwrap_response_5b {
my ($this,$response,$tag,$oids,$errorp) = @_;
my ($community,$request_id,@rest,$snmpver);
($snmpver,$community,$request_id,
$this->{error_status},
$this->{error_index},
@rest)
= decode_by_template ($response, "%{%i%s%*{%i%i%i%{%@",
$tag);
return $this->ber_error ("Error decoding response PDU")
unless defined $snmpver;
return $this->error ("Received SNMP response with unknown snmp-version field $snmpver")
unless $snmpver == $this->snmp_version;
if ($this->{error_status} != 0) {
if ($errorp) {
my ($oid, $errmsg);
$errmsg = $error_status_code[$this->{error_status}] || $this->{error_status};
$oid = $oids->[$this->{error_index}-1]
if $this->{error_index} > 0 && $this->{error_index}-1 <= $#{$oids};
$oid = $oid->[0]
if ref($oid) eq 'ARRAY';
return ($community, $request_id,
$this->error ("Received SNMP response with error code\n"
." error status: $errmsg\n"
." index ".$this->{error_index}
.(defined $oid
? " (OID: ".&BER::pretty_oid($oid).")"
: "")));
} else {
if ($this->{error_index} == 1) {
@rest[$this->{error_index}-1..$this->{error_index}] = ();
}
}
}
($community, $request_id, @rest);
}
sub send_query ($$) {
my ($this,$query) = @_;
send ($this->sock,$query,0,$this->remote_addr);
}
## Compare two sockaddr_in structures for equality. This is used when
## matching incoming responses with outstanding requests. Previous
## versions of the code simply did a bytewise comparison ("eq") of the
## two sockaddr_in structures, but this didn't work on some systems
## where sockaddr_in contains other elements than just the IP address
## and port number, notably FreeBSD.
##
## We allow for varying degrees of leniency when checking the source
## address. By default we now ignore it altogether, because there are
## agents that don't respond from UDP port 161, and there are agents
## that don't respond from the IP address the query had been sent to.
##
sub sa_equal_p ($$$) {
my ($this, $sa1, $sa2) = @_;
my ($p1, $a1) = sockaddr_in ($sa1);
my ($p2, $a2) = sockaddr_in ($sa2);
if (! $this->{'lenient_source_address_matching'}) {
return 0 if $a1 ne $a2;
}
if (! $this->{'lenient_source_port_matching'}) {
return 0 if $p1 != $p2;
}
return 1;
}
sub receive_response_3 {
my ($this, $response_tag, $oids, $errorp) = @_;
my ($remote_addr);
$remote_addr = recv ($this->sock,$this->{'pdu_buffer'},$this->max_pdu_len,0);
return $this->error ("receiving response PDU: $!")
unless defined $remote_addr;
return $this->error ("short (".length $this->{'pdu_buffer'}
." bytes) response PDU")
unless length $this->{'pdu_buffer'} > 2;
my $response = $this->{'pdu_buffer'};
##
## Check whether the response came from the address we've sent the
## request to. If this is not the case, we should probably ignore
## it, as it may relate to another request.
##
if (defined $this->{'remote_addr'}) {
if (! $this->sa_equal_p ($remote_addr, $this->{'remote_addr'})) {
if ($this->{'debug'} && !$SNMP_Session::recycle_socket) {
carp ("Response came from ".&SNMP_Session::pretty_address($remote_addr)
.", not ".&SNMP_Session::pretty_address($this->{'remote_addr'}))
unless $SNMP_Session::suppress_warnings;
}
return 0;
}
}
$this->{'last_sender_addr'} = $remote_addr;
my ($response_community, $response_id, @unwrapped)
= $this->unwrap_response_5b ($response, $response_tag,
$oids, $errorp);
if ($response_community ne $this->{community}
|| $response_id ne $this->{request_id}) {
if ($this->{'debug'}) {
carp ("$response_community != $this->{community}")
unless $SNMP_Session::suppress_warnings
|| $response_community eq $this->{community};
carp ("$response_id != $this->{request_id}")
unless $SNMP_Session::suppress_warnings
|| $response_id == $this->{request_id};
}
return 0;
}
if (!defined $unwrapped[0]) {
$this->{'unwrapped'} = undef;
return undef;
}
$this->{'unwrapped'} = \@unwrapped;
return length $this->pdu_buffer;
}
sub receive_trap {
my ($this) = @_;
my ($remote_addr, $iaddr, $port, $trap);
$remote_addr = recv ($this->sock,$this->{'pdu_buffer'},$this->max_pdu_len,0);
return undef unless $remote_addr;
($port, $iaddr) = sockaddr_in($remote_addr);
$trap = $this->{'pdu_buffer'};
return ($trap, $iaddr, $port);
}
sub describe {
my($this) = shift;
print $this->to_string (),"\n";
}
sub to_string {
my($this) = shift;
my ($class,$prefix);
$class = ref($this);
$prefix = ' ' x (length ($class) + 2);
($class
.(defined $this->{remote_hostname}
? " (remote host: \"".$this->{remote_hostname}."\""
." ".&SNMP_Session::pretty_address ($this->remote_addr).")"
: " (no remote host specified)")
."\n"
.$prefix." community: \"".$this->{'community'}."\"\n"
.$prefix." request ID: ".$this->{'request_id'}."\n"
.$prefix."PDU bufsize: ".$this->{'max_pdu_len'}." bytes\n"
.$prefix." timeout: ".$this->{timeout}."s\n"
.$prefix." retries: ".$this->{retries}."\n"
.$prefix." backoff: ".$this->{backoff}.")");
## sprintf ("SNMP_Session: %s (size %d timeout %g)",
## &SNMP_Session::pretty_address ($this->remote_addr),$this->max_pdu_len,
## $this->timeout);
}
### SNMP Agent support
### contributed by Mike McCauley <mikem@open.com.au>
###
sub receive_request {
my ($this) = @_;
my ($remote_addr, $iaddr, $port, $request);
$remote_addr = recv($this->sock, $this->{'pdu_buffer'},
$this->{'max_pdu_len'}, 0);
return undef unless $remote_addr;
($port, $iaddr) = sockaddr_in($remote_addr);
$request = $this->{'pdu_buffer'};
return ($request, $iaddr, $port);
}
sub decode_request {
my ($this, $request) = @_;
my ($snmp_version, $community, $requestid, $errorstatus, $errorindex, $bindings);
($snmp_version, $community, $requestid, $errorstatus, $errorindex, $bindings)
= decode_by_template ($request, "%{%i%s%*{%i%i%i%@", SNMP_Session::get_request);
if (defined $snmp_version)
{
# Its a valid get_request
return(SNMP_Session::get_request, $requestid, $bindings, $community);
}
($snmp_version, $community, $requestid, $errorstatus, $errorindex, $bindings)
= decode_by_template ($request, "%{%i%s%*{%i%i%i%@", SNMP_Session::getnext_request);
if (defined $snmp_version)
{
# Its a valid getnext_request
return(SNMP_Session::getnext_request, $requestid, $bindings, $community);
}
($snmp_version, $community, $requestid, $errorstatus, $errorindex, $bindings)
= decode_by_template ($request, "%{%i%s%*{%i%i%i%@", SNMP_Session::set_request);
if (defined $snmp_version)
{
# Its a valid set_request
return(SNMP_Session::set_request, $requestid, $bindings, $community);
}
# Something wrong with this packet
# Decode failed
return undef;
}
package SNMPv2c_Session;
use strict qw(vars subs); # see above
use vars qw(@ISA);
use SNMP_Session;
use BER;
use Carp;
@ISA = qw(SNMPv1_Session);
sub snmp_version { 1 }
sub open {
my $session = SNMPv1_Session::open (@_);
return bless $session;
}
## map_table_start_end using get-bulk
##
sub map_table_start_end ($$$$$$) {
my ($session, $columns, $mapfn, $start, $end, $max_repetitions) = @_;
my @encoded_oids;
my $call_counter = 0;
my $base_index = $start;
my $ncols = @{$columns};
my @collected_values = ();
if (! $session->{'use_getbulk'}) {
return SNMP_Session::map_table_start_end
($session, $columns, $mapfn, $start, $end, $max_repetitions);
}
$max_repetitions = $session->default_max_repetitions
unless defined $max_repetitions;
for (;;) {
foreach (@encoded_oids = @{$columns}) {
$_=encode_oid (@{$_},split '\.',$base_index)
|| return $session->ber_error ("encoding OID $base_index");
}
if ($session->getbulk_request_response (0, $max_repetitions,
@encoded_oids)) {
my $response = $session->pdu_buffer;
my ($bindings) = $session->decode_get_response ($response);
my @colstack = ();
my $k = 0;
my $j;
my $min_index = undef;
my @bases = @{$columns};
my $n_bindings = 0;
my $binding;
## Copy all bindings into the colstack.
## The colstack is a vector of vectors.
## It contains one vector for each "repeater" variable.
##
while ($bindings ne '') {
($binding, $bindings) = decode_sequence ($bindings);
my ($oid, $value) = decode_by_template ($binding, "%O%@");
push @{$colstack[$k]}, [$oid, $value];
++$k; $k = 0 if $k >= $ncols;
}
## Now collect rows from the column stack:
##
## Iterate through the column stacks to find the smallest
## index, collecting the values for that index in
## @collected_values.
##
## As long as a row can be assembled, the map function is
## called on it and the iteration proceeds.
##
$base_index = undef;
walk_rows_from_pdu:
for (;;) {
my $min_index = undef;
for ($k = 0; $k < $ncols; ++$k) {
$collected_values[$k] = undef;
my $pair = $colstack[$k]->[0];
unless (defined $pair) {
$min_index = undef;
last walk_rows_from_pdu;
}
my $this_index
= SNMP_Session::oid_diff ($columns->[$k], $pair->[0]);
if (defined $this_index) {
my $cmp
= !defined $min_index
? -1
: SNMP_Session::index_compare
($this_index, $min_index);
if ($cmp == -1) {
for ($j = 0; $j < $k; ++$j) {
unshift (@{$colstack[$j]},
[$min_index,
$collected_values[$j]]);
$collected_values[$j] = undef;
}
$min_index = $this_index;
}
if ($cmp <= 0) {
$collected_values[$k] = $pair->[1];
shift @{$colstack[$k]};
}
}
}
($base_index = undef), last
if !defined $min_index;
last if defined $end && index_compare ($min_index, $end) >= 0;
&$mapfn ($min_index, @collected_values);
++$call_counter;
$base_index = $min_index;
}
} else {
return undef;
}
last if !defined $base_index;
last if defined $end and index_compare ($base_index, $end) >= 0;
}
$call_counter;
}
1;
+11
View File
@@ -0,0 +1,11 @@
<html>
<head>
</head>
<body>
<a href="runon.pl">Wochentage an denen Checks laufen sollen</a><br>
<a href="notrunon.pl">Feiertage an denen Checks nicht laufen sollen</a><br>
<a href="del_alerts_log.pl">Alerts-Log Tabelle leeren</a><br>
</body>
</html>
+24
View File
@@ -0,0 +1,24 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<meta http-equiv='refresh' content='10; alerts_log.pl'>\n";
print "</head>\n";
print "<body>\n";
my $sth = $dbh->prepare("select date,time,msg,host_hostname,service_result from alerts_log order by date desc, time desc");
$sth->execute;
print "<table border='1'>\n";
print "<tr><td>Datum</td><td>Zeit</td><td>Nachricht</td><td>Hostname</td><td>Dienstergebnis</td></tr>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr><td>$ref->[0]</td><td>$ref->[1]</td><td>$ref->[2]</td><td>$ref->[3]</td><td>$ref->[4]</td></tr>\n";
}
print "</table>\n";
print "</body>\n";
print "</html>\n";
+675
View File
@@ -0,0 +1,675 @@
#!/bin/perl
use Mysql;
use strict;
use Net::SNMP;
use Net::Ping;
use BER;
require 'SNMP_Session.pm';
use Data::Dumper;
our ($c2,$dbh);
main();
sub main {
$dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
while (1) {
read_database();
$c2->{'time'} = get_time();
$c2->{'date'} = get_date();
print "$c2->{'date'} $c2->{'time'}\n";
if(run_now()) {
foreach my $todo (keys %{$c2->{'todo'}}) {
get_service_value($todo);
}
#open TEST, ">TEST.txt";
#print TEST Dumper ($c2);
#close TEST;
create_web_from_template();
}
print "\n";
sleep ($c2->{'conf'}->{'check_intervall'});
}
}
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, ">index.html";
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'}/;
#<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! => 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'};
my ($session,$error) = Net::SNMP->session(Hostname => "$host", Community => "$community", Port => $port);
$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 (snmp request)");
$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 (ping)");
}
}
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 = 1;
my $bytes = 32;
my $ok = 0;
my $ping = Net::Ping->new($pingtype,$timeout,$bytes);
if ($ping->ping($host,1)) {
$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'}} ) {
$sth = $dbh->prepare("select ind from todos where t_active='1'");
$sth->execute;
# print "$todo:";
while ($ref = $sth->fetchrow_arrayref()) {
my $found = 0;
if ($ref->[0] == $todo) {
$found=1;
}
unless ($found) {
delete $c2->{'todo'}->{$todo};
}
}
}
# 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;
}
+87
View File
@@ -0,0 +1,87 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $ind = param('ind');
my $akt = param('akt');
my $deakt = param('deakt');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Todo aktivieren/deaktivieren</u></b><br><br>\n";
if ($ind eq "") {
my $sth = $dbh->prepare("select ind,t_host,t_service,t_check,t_alert,t_description from todos order by 'ind'");
$sth->execute;
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<form action='chg_todo.pl'>\n";
print "<tr>\n";
print "<td><input type='hidden' name='ind' value='$ref->[0]'></td>\n";
#HOST
my $sth_host = $dbh->prepare("select hostname,ip from hosts where ind='$ref->[1]'");
$sth_host->execute;
while (my $ref_host = $sth_host->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_host->[0] ($ref_host->[1])'></td>\n";
}
#SERVICE
my $sth_desc = $dbh->prepare("select distinct description from services where service_ind='$ref->[2]'");
$sth_desc->execute;
while (my $ref_desc = $sth_desc->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_desc->[0]'></td>\n";
}
#CHECK
my $sth_check = $dbh->prepare("select description from checks where ind='$ref->[3]'");
$sth_check->execute;
while (my $ref_check = $sth_check->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_check->[0]'></td>\n";
}
#ALERT
my $sth_alert = $dbh->prepare("select description from alerts where ind='$ref->[4]'");
$sth_alert->execute;
while (my $ref_alert = $sth_alert->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_alert->[0]'></td>\n";
}
#DESCRIPTION
print "<td><input type='text' readonly value='$ref->[5]'></td>\n";
print "<td><input type='submit' name='akt' value='aktivieren'></td>\n";
print "<td><input type='submit' name='deakt' value='deaktivieren'></td>\n";
print "</tr>\n";
print "</form>\n";
}
print "</table>\n";
}
else {
#$dbh->do("delete from todos where ind='$del'");
if ($akt eq "aktivieren") {
$dbh->do("update todos set t_active = '1' where ind ='$ind'");
print "<br>Todo aktiviert!\n";
}
if ($deakt eq "deaktivieren") {
$dbh->do("update todos set t_active = '0' where ind ='$ind'");
print "<br>Todo deaktiviert!\n";
}
}
print "</body>\n";
print "</html>\n";
+44
View File
@@ -0,0 +1,44 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $ind = param('ind');
my $del = param('del');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Alarm entfernen</u></b>\n";
if ($del eq "") {
my $sth = $dbh->prepare("select ind,description,alert_type from alerts order by 'alert_typ','description'");
$sth->execute;
print "<form action='del_alert.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='del' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' value='Löschen'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
$dbh->do("delete from alerts where ind='$del'");
print "<br>Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+16
View File
@@ -0,0 +1,16 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "</head>\n";
print "<body>\n";
$dbh->do("delete from alerts_log");
print "<br><br>Alerts_log geleert!\n";
print "</body>\n";
print "</html>\n";
+45
View File
@@ -0,0 +1,45 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $ind = param('ind');
my $del = param('del');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Check entfernen</u></b>\n";
if ($del eq "") {
my $sth = $dbh->prepare("select ind,description,beding,wert from checks order by 'description'");
$sth->execute;
print "<form action='del_check.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='del' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "<td><input type='text' readonly value='$ref->[3]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' value='Löschen'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
$dbh->do("delete from checks where ind='$del'");
print "<br>Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+45
View File
@@ -0,0 +1,45 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $ind = param('ind');
my $del = param('del');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Host entfernen</u></b>\n";
if ($del eq "") {
my $sth = $dbh->prepare("select ind,description,hostname,ip from hosts order by 'hostname'");
$sth->execute;
print "<form action='del_host.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='del' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "<td><input type='text' readonly value='$ref->[3]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' value='Löschen'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
$dbh->do("delete from hosts where ind='$del'");
print "<br>Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+74
View File
@@ -0,0 +1,74 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $ind = param('ind');
my $del = param('del');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Todo entfernen</u></b>\n";
if ($del eq "") {
my $sth = $dbh->prepare("select ind,t_host,t_service,t_check,t_alert,t_description from todos order by 'ind'");
$sth->execute;
print "<form action='del_todo.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='del' value='$ref->[0]'></td>\n";
#HOST
my $sth_host = $dbh->prepare("select hostname,ip from hosts where ind='$ref->[1]'");
$sth_host->execute;
while (my $ref_host = $sth_host->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_host->[0] ($ref_host->[1])'></td>\n";
}
#SERVICE
my $sth_desc = $dbh->prepare("select distinct description from services where service_ind='$ref->[2]'");
$sth_desc->execute;
while (my $ref_desc = $sth_desc->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_desc->[0]'></td>\n";
}
#CHECK
my $sth_check = $dbh->prepare("select description from checks where ind='$ref->[3]'");
$sth_check->execute;
while (my $ref_check = $sth_check->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_check->[0]'></td>\n";
}
#ALERT
my $sth_alert = $dbh->prepare("select description from alerts where ind='$ref->[4]'");
$sth_alert->execute;
while (my $ref_alert = $sth_alert->fetchrow_arrayref()) {
print "<td><input type='text' readonly value='$ref_alert->[0]'></td>\n";
}
#DESCRIPTION
print "<td><input type='text' readonly value='$ref->[5]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' value='Löschen'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
$dbh->do("delete from todos where ind='$del'");
print "<br>Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+137
View File
@@ -0,0 +1,137 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $edit = param('edit');
my $edit1 = param('edit1');
my $descr=param('descr');
my $typ=param('typ');
my $mf=param('mf');
my $mt=param('mt');
my $se=param('se');
my $sg=param('sg');
my $ss=param('ss');
my $st=param('st');
my $sp=param('sp');
my $sc=param('sc');
my $sm=param('sm');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
#print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Alert editieren</u></b>\n";
if ($edit eq "") {
my $sth = $dbh->prepare("select ind,description,alert_type from alerts order by 'description'");
$sth->execute;
print "<form action='edit_alert.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='edit' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' name='edit1' value='Edit'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
if ($edit1 eq "Edit") {
my $sth = $dbh->prepare("select ind,description,alert_type,mail_from,mail_to,enterprise,generic,specific,destination,port,community,txt_msg from alerts where ind='$edit'");
$sth->execute;
while (my $ref = $sth->fetchrow_arrayref()) {
print "<form action='edit_alert.pl'>\n";
print "<table border='1'>\n";
print "<tr>\n<td>Description</td>\n";
print "<td><input name='descr' type='text' value='$ref->[1]'></td>\n";
print "</tr>\n";
#ääää
print "<tr>\n";
print "<td>Typ</td>\n";
print "<td><select name='typ'>\n";
if ($ref->[2] eq "mail") {
print "<option value='snmp'>SNMP</option>\n";
print "<option selected value='mail'>MAIL</option>\n";
}
if ($ref->[2] eq "snmp") {
print "<option selected value='snmp'>SNMP</option>\n";
print "<option value='mail'>MAIL</option>\n";
}
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>Mail: from</td>\n";
print "<td><input name='mf' type='text' value='$ref->[3]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>Mail: to</td>\n";
print "<td><input name='mt' type='text' value='$ref->[4]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: Enterprise</td>\n";
print "<td><input name='se' type='text' value='$ref->[5]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: generic</td>\n";
print "<td><input name='sg' type='text' value='$ref->[6]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: specific</td>\n";
print "<td><input name='ss' type='text' value='$ref->[7]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: to</td>\n";
print "<td><input name='st' type='text' value='$ref->[8]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: Port</td>\n";
print "<td><input name='sp' type='text' value='$ref->[9]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP: Community</td>\n";
print "<td><input name='sc' type='text' value='$ref->[10]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP+Mail: Nachricht</td>\n";
print "<td><input name='sm' type='text' value='$ref->[11]'></td>\n";
print "</tr>\n";
print "</table>\n";
print "<input type='submit' name='edit1' value='Speichern'>\n";
print "<input type='hidden' name='edit' value='$edit'>\n";
print "</form>\n";
}
}
if ($edit1 eq "Speichern") {
#$dbh->do("update hosts set description='$descr', hostname='$hostn', ip='$ipadd', rw_community='$rwcom', ro_community='$rocom', snmp_port='$snmpp' where ind='$edit'");
#$dbh->do(
$dbh->do("update alerts set description='$descr', alert_type='$typ', mail_from='$mf', mail_to='$mt', enterprise='$se', generic='$sg', specific='$ss', destination='$st', port='$sp', community='$sc', txt_msg='$sm' where ind='$edit'");
print "<br>Gespeichert\n";
}
#print "Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+106
View File
@@ -0,0 +1,106 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $edit = param('edit');
my $edit1 = param('edit1');
my $descr=param('descr');
my $bedin=param('bedingung');
my $wert=param('wert');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
#print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Check editieren</u></b>\n";
if ($edit eq "") {
my $sth = $dbh->prepare("select ind,description,beding,wert from checks order by 'description'");
$sth->execute;
print "<form action='edit_check.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='edit' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "<td><input type='text' readonly value='$ref->[3]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' name='edit1' value='Edit'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
if ($edit1 eq "Edit") {
my $sth = $dbh->prepare("select ind,description,beding,wert from checks where ind='$edit'");
$sth->execute;
while (my $ref = $sth->fetchrow_arrayref()) {
print "<form action='edit_check.pl'>\n";
print "<table border='1'>\n";
print "<tr>\n<td>Description</td>\n";
print "<td><input name='descr' type='text' value='$ref->[1]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>Prüfung</td>\n";
print "<td>";
print "<select name='bedingung' value='$ref->[2]'>\n";
my $seltxt="";
if ($ref->[2] eq "EQ") { $seltxt=" selected"; }
print "<option $seltxt>EQ</option>\n";
$seltxt="";
if ($ref->[2] eq "NEQ") { $seltxt=" selected"; }
print "<option $seltxt>NEQ</option>\n";
$seltxt="";
if ($ref->[2] eq "LT") { $seltxt=" selected"; }
print "<option $seltxt>LT</option>\n";
$seltxt="";
if ($ref->[2] eq "NLT") { $seltxt=" selected"; }
print "<option $seltxt>NLT</option>\n";
$seltxt="";
if ($ref->[2] eq "GT") { $seltxt=" selected"; }
print "<option $seltxt>GT</option>\n";
$seltxt="";
if ($ref->[2] eq "NGT") { $seltxt=" selected"; }
print "<option $seltxt>NGT</option>\n";
$seltxt="";
if ($ref->[2] eq "BT") { $seltxt=" selected"; }
print "<option $seltxt>BT</option>\n";
$seltxt="";
if ($ref->[2] eq "NBT") { $seltxt=" selected"; }
print "<option $seltxt>NBT</option>\n";
$seltxt="";
if ($ref->[2] eq "RE") { $seltxt=" selected"; }
print "<option $seltxt>RE</option>\n";
print "</select>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>Vergleichswert</td>\n";
print "<td><input name='wert' type='text' value='$ref->[3]'></td>\n";
print "</tr>\n";
print "</table>\n";
print "<input type='submit' name='edit1' value='Speichern'>\n";
print "<input type='hidden' name='edit' value='$edit'>\n";
print "</form>\n";
}
}
if ($edit1 eq "Speichern") {
$dbh->do("update checks set description='$descr', beding='$bedin', wert='$wert' where ind='$edit'");
#print "update checks set description='$descr', beding='$bedin', wert='$wert' where ind='$edit'";
print "<br>Gespeichert\n";
}
#print "Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+84
View File
@@ -0,0 +1,84 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $edit = param('edit');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
my $ci=param('ci');
my $we=param('we');
my $wri=param('wri');
my $url=param('url');
my $urla=param('urla');
my $path=param('path');
my $roi=param('roi');
my $wt=param('wt');
my $starttime=param('starttime');
my $stoptime=param('stoptime');
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
#print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Konfig editieren</u></b>\n";
if ($edit eq "") {
my $sth = $dbh->prepare("select check_intervall,web_enable,web_refresh_intervall,web_link,web_template,runs_on_ip,config_url,start_time,stop_time,index_dir from conf");
$sth->execute;
print "<form action='edit_conf.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td>Check Intervall</td><td><input name='ci' type='text' value='$ref->[0]'></td>\n";
print "<tr></tr>\n";
print "<td>Web enabled</td>\n";
print "<td><select name='we'>\n";
if ($ref->[1] eq "1") {
print "<option selected value='1'>Ja</option>\n";
print "<option value='0'>Nein</option>\n";
}
if ($ref->[1] eq "0") {
print "<option value='1'>Ja</option>\n";
print "<option selected value='0'>Nein</option>\n";
}
print "</td>\n";
print "<tr></tr>\n";
print "<td>Web refresh Intervall</td><td><input name='wri' type='text' value='$ref->[2]'></td>\n";
print "<tr></tr>\n";
print "<td>Web URL</td><td><input size='40' name='url' type='text' value='$ref->[3]'></td>\n";
print "<tr></tr>\n";
print "<td>Web Template</td><td><input size='60' name='wt' type='text' value='$ref->[4]'></td>\n";
print "<tr></tr>\n";
print "<td>Web-Page</td><td><input size='60' name='path' type='text' value='$ref->[9]'></td>\n";
print "<tr></tr>\n";
print "<td>Läuft auf IP</td><td><input name='roi' type='text' value='$ref->[5]'></td>\n";
print "<tr></tr>\n";
print "<td>Web Admin URL</td><td><input name='urla' size='40' type='text' value='$ref->[6]'></td>\n";
print "<tr></tr>\n";
print "<td>Startzeit</td><td><input name='starttime' size='10' type='text' value='$ref->[7]'></td>\n";
print "<tr></tr>\n";
print "<td>Stopzeit</td><td><input name='stoptime' size='10' type='text' value='$ref->[8]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' name='edit' value='Speichern'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
if ($edit eq "Speichern") {
$dbh->do("update conf set index_dir='$path',check_intervall='$ci',web_enable='$we',web_refresh_intervall='$wri',web_link='$url',web_template='$wt',runs_on_ip='$roi',config_url='$urla',start_time='$starttime',stop_time='$stoptime' where active='1'");
#print "update conf set check_intervall='$ci',web_enable='$we',web_refresh_intervall='$wri',web_link='$url',web_template='$wt',runs_on_ip='$roi',config_url='$urla' where active='1'";
# where active='1'
print "<br><br>Gespeichert\n";
}
}
print "</body>\n";
print "</html>\n";
+91
View File
@@ -0,0 +1,91 @@
#!/bin/perl
use strict;
use mysql;
use CGI qw(:standard);
my $edit = param('edit');
my $edit1 = param('edit1');
my $descr=param('descr');
my $hostn=param('hostname');
my $ipadd=param('ip');
my $rocom=param('roc');
my $rwcom=param('rwc');
my $snmpp=param('port');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Host editieren</u></b>\n";
if ($edit eq "") {
my $sth = $dbh->prepare("select ind,description,hostname,ip from hosts order by 'hostname'");
$sth->execute;
print "<form action='edit_host.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr>\n";
print "<td><input type='radio' name='edit' value='$ref->[0]'></td>\n";
print "<td><input type='text' readonly value='$ref->[1]'></td>\n";
print "<td><input type='text' readonly value='$ref->[2]'></td>\n";
print "<td><input type='text' readonly value='$ref->[3]'></td>\n";
print "</tr>\n";
}
print "<tr><td></td>\n<td><input type='submit' name='edit1' value='Edit'></td></tr>\n";
print "</table>\n";
print "</form>\n";
}
else {
if ($edit1 eq "Edit") {
my $sth = $dbh->prepare("select ind,description,hostname,ip,rw_community,ro_community,snmp_port from hosts where ind='$edit'");
$sth->execute;
while (my $ref = $sth->fetchrow_arrayref()) {
print "<form action='edit_host.pl'>\n";
print "<table border='1'>\n";
print "<tr>\n<td>Description</td>\n";
print "<td><input name='descr' type='text' value='$ref->[1]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>Hostname</td>\n";
print "<td><input name='hostname' type='text' value='$ref->[2]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>IP</td>\n";
print "<td><input name='ip' type='text' value='$ref->[3]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>RO-community</td>\n";
print "<td><input name='roc' type='text' value='$ref->[5]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>RW-community</td>\n";
print "<td><input name='rwc' type='text' value='$ref->[4]'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td>SNMP-Port</td>\n";
print "<td><input name='port' type='text' value='$ref->[6]'></td>\n";
print "</tr>\n";
print "</table>\n";
print "<input type='submit' name='edit1' value='Speichern'>\n";
print "<input type='hidden' name='edit' value='$edit'>\n";
print "</form>\n";
}
}
if ($edit1 eq "Speichern") {
$dbh->do("update hosts set description='$descr', hostname='$hostn', ip='$ipadd', rw_community='$rwcom', ro_community='$rocom', snmp_port='$snmpp' where ind='$edit'");
#$dbh->do(
print "<br>Gespeichert\n";
}
#print "Löschen durchgeführt!";
}
print "</body>\n";
print "</html>\n";
+11
View File
@@ -0,0 +1,11 @@
<html>
<head>
<title>Admin</title>
</head>
<body>
<b><u>Todo editieren</u></b><br><br>
Bitte Todo löschen und einen neuen anlegen!<br><br>
Falls der alte Todo evtl. noch einmal gebraucht werden sollte,
lässt sich dieser auch vorübergehend deaktivieren.
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
<html>
<head>
<title>Administration</title>
</head>
<frameset cols='230,*' rows='100%' border="0" frameborder="1" framespacing="0">
<frame src="links.html" name='links' scrolling="Option" noresize>
<frame src='rechts.html' name='rechts'>
</frameset>
</html>
+40
View File
@@ -0,0 +1,40 @@
<html>
<head>
<title>Admin</title>
</head>
<body>
<a href="admin.html" target='rechts'>Administration</a><br>
<br>
<a href="alerts_log.pl" target='rechts'>Alarme</a><br>
<br>
<a href="edit_conf.pl" target='rechts'>Konfig editieren</a><br>
<br>
<a href="new_host.html" target='rechts'>Host hinzufügen</a><br>
<a href="del_host.pl" target='rechts'>Host entfernen</a><br>
<a href="edit_host.pl" target='rechts'>Host editieren</a><br>
<br>
<a href="new_service.html" target='rechts'>Dienst hinzufügen</a><br>
<a href="del_service.pl" target='rechts'>Dienst entfernen</a><br>
<a href="edit_service.pl" target='rechts'>Dienst editieren</a><br>
<br>
<a href="new_check.html" target='rechts'>Check hinzufügen</a><br>
<a href="del_check.pl" target='rechts'>Check entfernen</a><br>
<a href="edit_check.pl" target='rechts'>Check editieren</a><br>
<br>
<a href="new_alert.html" target='rechts'>Alarm hinzufügen</a><br>
<a href="del_alert.pl" target='rechts'>Alarm entfernen</a><br>
<a href="edit_alert.pl" target='rechts'>Alarm editieren</a><br>
<br>
<a href="new_todo.pl" target='rechts'>Todo hinzufügen</a><br>
<a href="del_todo.pl" target='rechts'>Todo entfernen</a><br>
<a href="edit_todo.html" target='rechts'>Todo editieren</a><br>
<a href="chg_todo.pl" target='rechts'>Todo aktivieren/deaktivieren</a><br>
</body>
</html>
+66
View File
@@ -0,0 +1,66 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Alarm hinzufügen</title>
</head>
<body>
<b><u>Alarm hinzufügen</u></b>
<form action="save_alert.pl">
<table border="1">
<tr>
<td>Description</td>
<td><input name="descr" type="text" value=""></td>
</tr>
<tr>
<td>Typ</td>
<td>
<select name="typ">
<option value="snmp">SNMP</option>
<option value="mail">MAIL</option>
</select>
</td>
</tr>
<tr>
<td>Mail: from</td>
<td><input name="mfrom" type="text" value=""></td>
</tr>
<tr>
<td>Mail: to</td>
<td><input name="mto" type="text" value=""></td>
</tr>
<tr>
<td>SNMP: Enterprise</td>
<td><input name="enterprise" type="text" value="1.3.6.1.4.1.2222"></td>
</tr>
<tr>
<td>SNMP: generic</td>
<td><input name="generic" type="text" value="6"></td>
</tr>
<tr>
<td>SNMP: specific</td>
<td><input name="specific" type="text" value=""></td>
</tr>
<tr>
<td>SNMP: to</td>
<td><input name="sto" type="text" value=""></td>
</tr>
<tr>
<td>SNMP: Port</td>
<td><input name="port" type="text" value="162"></td>
</tr>
<tr>
<td>SNMP: Community</td>
<td><input name="comm" type="text" value="public"></td>
</tr>
<tr>
<td>SNMP+Mail: Nachricht</td>
<td><input name="msg" type="text" value="Fehler!"></td>
</tr>
</table>
<input type="submit" value="speichern">
</form>
</body>
</html>
+41
View File
@@ -0,0 +1,41 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Check hinzufügen</title>
</head>
<body>
<b><u>Check hinzufügen</u></b>
<form action="save_check.pl">
<table border="1">
<tr>
<td>Description</td>
<td><input name="descr" type="text" value=""></td>
</tr>
<tr>
<td>Prüfung</td>
<td>
<select name="if">
<option>EQ</option>
<option>NEQ</option>
<option>LT</option>
<option>NLT</option>
<option>GT</option>
<option>NGT</option>
<option>BT</option>
<option>NBT</option>
<option>RE</option>
</select>
</td>
</tr>
<tr>
<td>Vergleichswert</td>
<td><input name="value" type="text" value=""></td>
</tr>
</table>
<input type="submit" value="speichern">
</form>
</body>
</html>
+39
View File
@@ -0,0 +1,39 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Host hinzufügen</title>
</head>
<body>
<b><u>Host hinzufügen</u></b>
<form action="save_host.pl">
<table border="1">
<tr>
<td>Description</td>
<td><input name="descr" type="text" value=""></td>
</tr>
<tr>
<td>Hostname</td>
<td><input name="hostname" type="text" value=""></td>
</tr>
<tr>
<td>IP</td>
<td><input name="ip" type="text" value=""></td>
</tr>
<tr>
<td>RO-community</td>
<td><input name="roc" type="text" value="public"></td>
</tr>
<tr>
<td>RW-community</td>
<td><input name="rwc" type="text" value=""></td>
</tr>
<tr>
<td>SNMP-Port</td>
<td><input name="port" type="text" value="161"></td>
</tr>
</table>
<input type="submit" value="speichern">
</form>
</body>
</html>
+81
View File
@@ -0,0 +1,81 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Dienst hinzufügen</title>
</head>
<body>
<b><u>Dienst hinzufügen</u></b>
<form action="save_service.pl">
<table border="1">
<tr>
<td>Description</td>
<td><input name="descr" type="text" value=""></td>
</tr>
<tr>
<td>Service-Nummer</td>
<td><input name="sid" type="text" value=""></td>
</tr>
<tr>
<td>Nummer im Dienst</td>
<td><input name="pid" type="text" value=""></td>
</tr>
<tr>
<td>Aufgabe</td>
<td>
<table>
<tr>
<td><input checked name="check" type="radio" value="ping"></td>
<td>P</td>
<td>
<select name="ping">
<option>IP</option>
<option>HOST</option>
</select>
</td>
</tr>
<tr>
<td><input name="check" type="radio" value="snmp"></td>
<td>S</td>
<td><input name="snmp" type="text" value=""></td>
</tr>
<tr>
<td><input name="check" type="radio" value="konst"></td>
<td>K</td>
<td><input name="konst" type="text" value=""></td>
</tr>
<tr>
<td><input name="check" type="radio" value="calc"></td>
<td>C</td>
<td>
<input name="calc1" type="text" value="">
<select name="calc2">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input name="calc3" type="text" value="">
</td>
</tr>
<tr>
<td><input name="check" type="radio" value="result"></td>
<td>R</td>
<td>
<select name="result1">
<option>A</option>
<option>R</option>
</select>-
<input name="result2" type="text" value=""></td>
</tr>
</table>
</td>
</tr>
</table>
<input type="submit" value="speichern">
</body>
</html>
+94
View File
@@ -0,0 +1,94 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $gruppe = param('gruppe');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
my ($sth,$ref);
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
#print "td { font-size:10pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<b><u>Todo hinzufügen</u></b>\n";
print "<form action='save_todo.pl'>\n";
print " <table border='1'>\n";
print " <tr>\n";
print " <td>Host</td>\n";
print " <td>Service</td>\n";
print " <td>Prüfung</td>\n";
print " <td>Alarm</td>\n";
print " <td>Aktiv?</td>\n";
print " <td>Beschreibung</td>\n";
print " </tr>\n";
print " <tr>\n";
print " <td>\n";
print " <select name='host'>\n";
# hosts
$sth = $dbh->prepare("select ind,hostname,ip from hosts order by hostname");
$sth->execute;
while ($ref = $sth->fetchrow_arrayref()) {
print " <option value='$ref->[0]'><font size='-1'>$ref->[1] ($ref->[2])</font></option>\n";
}
print " </select>\n";
print " </td>\n";
print " <td>\n";
print " <select name='service'>\n";
# services
$sth = $dbh->prepare("select distinct service_ind, description from services");
$sth->execute;
while ($ref = $sth->fetchrow_arrayref()) {
print " <option value='$ref->[0]'>$ref->[1]</option>\n";
}
print " </select>\n";
print " </td>\n";
print " <td>\n";
print " <select name='check'>\n";
#checks
$sth = $dbh->prepare("select description,beding,wert,ind from checks");
$sth->execute;
while ($ref = $sth->fetchrow_arrayref()) {
print " <option value='$ref->[3]'>$ref->[0] ($ref->[1] $ref->[2])</option>\n";
}
print " </select>\n";
print " </td>\n";
print " <td>\n";
print " <select name='alert'>\n";
# alerts
$sth = $dbh->prepare("select description,alert_type,ind from alerts");
$sth->execute;
while ($ref = $sth->fetchrow_arrayref()) {
print " <option value='$ref->[2]'>$ref->[0] ($ref->[1])</option>\n";
}
print " </select>\n";
print " </td>\n";
print " <td><select name='active'><option value='1'>ja</option><option value='0'>nein</option></select></td>\n";
print " <td><input maxlength='255' name='descr' type='text' value=''></td>\n";
print " </tr>\n";
print " </table>\n";
print " <input type='submit' value='speichern'>\n";
print "</form>\n";
print "\n";
# $sth = $dbh->prepare($sel);
# $sth->execute;
# while ($ref = $sth->fetchrow_arrayref()) {
# print "$ref->[1], $ref->[2] $ref->[3] <a target='rechts' href='forum_rechts.pl?index=$ref->[0]'>$ref->[4]</a>";
# }
print "</body>\n</html>\n";
+47
View File
@@ -0,0 +1,47 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $save=param('save');
my $dd = param('day');
my $mm = param('month');
my $yy = param('year');
my $ftag = param('ftag');
my $datum;
print header();
print "<html>\n";
print "<head>\n";
print "</head>\n";
print "<body>\n";
print "Datumsformat dd-mm-yyyy. Ist dd oder mm kleiner als 10,<br>muß eine führende 0 eingegeben werden.<br>\n";
print "Bei Eingabe eines ungültigen Datums wird 00-00-0000 gespeichert!<br><br>\n";
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
if ($save eq "") {
my $sth = $dbh->prepare("select description,datum from not_run_on order by datum");
$sth->execute;
#print "<form action='notrunon.pl'>\n";
print "<table>\n";
while (my $ref = $sth->fetchrow_arrayref()) {
my ($yy,$mm,$dd) = split /-/,$ref->[1];
print "<tr><form action='notrunon.pl'><input type='hidden' name='ftag' value='$ref->[0]'><td>$ref->[0]</td><td><input size='1' type='text' name='day' value='$dd'></td><td><input size='1' type='text' name='month' value='$mm'></td><td><input size='3' type='text' name='year' value='$yy'></td><td><input type='submit' name='save' value='Speichern'></td></form></tr>\n";
}
print "</table>\n";
#print "</form>\n";
}
if ($save eq "Speichern") {
$datum="$yy" . "-" . "$mm" . "-" . "$dd";
$dbh->do("update not_run_on set datum='$datum' where description='$ftag'");
print "<br><br>Gespeichert\n";
#print "$ftag $dd $mm $yy";
}
print "</body>\n";
print "</html>\n";
+8
View File
@@ -0,0 +1,8 @@
<html>
<head>
<title>Admin</title>
</head>
<body>
Eingabeseite
</body>
</html>
+101
View File
@@ -0,0 +1,101 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $save=param('save');
my $so = param('Sonntag');
my $mo = param('Montag');
my $di = param('Dienstag');
my $mi = param('Mittwoch');
my $don = param('Donnerstag');
my $fr = param('Freitag');
my $sa = param('Samstag');
print header();
print "<html>\n";
print "<head>\n";
print "</head>\n";
print "<body>\n";
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
if ($save eq "") {
print "<form action='runon.pl'>\n";
print "An welchen Tagen sollen Checks durchgeführt werden?\n";
print "<table>\n";
my $sth = $dbh->prepare("select description,day,value from run_on order by 'day'");
$sth->execute;
while (my $ref = $sth->fetchrow_arrayref()) {
print "<tr><td>$ref->[0]</td>\n";
print "<td>";
if ($ref->[2] == '1') { print "<input checked type='checkbox' name='$ref->[0]'>"; }
if ($ref->[2] == '0') { print "<input type='checkbox' name='$ref->[0]'>"; }
print "</td></tr>\n";
}
print "</table>\n";
print "<input type='hidden' name='save' value='save'>\n";
print "<input type='submit' value='Speichern'>\n";
print "</form>\n";
}
if ($save eq "save") {
if ($so) {
$dbh->do("update run_on set value = 1 where description = 'Sonntag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Sonntag'");
}
if ($mo) {
$dbh->do("update run_on set value = 1 where description = 'Montag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Montag'");
}
if ($di) {
$dbh->do("update run_on set value = 1 where description = 'Dienstag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Dienstag'");
}
if ($mi) {
$dbh->do("update run_on set value = 1 where description = 'Mittwoch'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Mittwoch'");
}
if ($don) {
$dbh->do("update run_on set value = 1 where description = 'Donnerstag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Donnerstag'");
}
if ($fr) {
$dbh->do("update run_on set value = 1 where description = 'Freitag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Freitag'");
}
if ($sa) {
$dbh->do("update run_on set value = 1 where description = 'Samstag'");
}
else {
$dbh->do("update run_on set value = 0 where description = 'Samstag'");
}
print "<br><br>Gespeichert\n";
}
print "</body>\n";
print "</html>\n";
+34
View File
@@ -0,0 +1,34 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $v0 = param('descr');
my $v1 = param('typ');
my $v2 = param('mfrom');
my $v3 = param('mto');
my $v4 = param('enterprise');
my $v5 = param('generic');
my $v6 = param('specific');
my $v7 = param('sto');
my $v8 = param('port');
my $v9 = param('comm');
my $v10 = param('msg');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
$dbh->do("insert into alerts (description,alert_type,mail_from,mail_to,enterprise,specific,generic,destination,port,community,txt_msg) values ('$v0','$v1','$v2','$v3','$v4','$v6','$v5','$v7','$v8','$v9','$v10')");
print "</body>\n</html>\n";
+26
View File
@@ -0,0 +1,26 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $v0 = param('descr');
my $v1 = param('if');
my $v2 = param('value');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
$dbh->do("insert into checks (description,beding,wert) values ('$v0','$v1','$v2')");
print "</body>\n</html>\n";
+29
View File
@@ -0,0 +1,29 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $v0 = param('descr');
my $v1 = param('hostname');
my $v2 = param('ip');
my $v3 = param('roc');
my $v4 = param('rwc');
my $v5 = param('port');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
$dbh->do("insert into hosts (description,hostname,ip,ro_community,rw_community,snmp_port) values ('$v0','$v1','$v2','$v3','$v4','$v5')");
print "</body>\n</html>\n";
+57
View File
@@ -0,0 +1,57 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $check=param('check');
my $v0 = param('descr');
my $v1 = param('sid');
my $v2 = param('pid');
my $ping=param('ping');
my $snmp=param('snmp');
my $konst=param('konst');
my $calc1=param('calc1');
my $calc2=param('calc2');
my $calc3=param('calc3');
my $result1=param('result1');
my $result2=param('result2');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
if ($check eq "ping") {
$dbh->do("insert into services (description,service_ind,position,todo,variable) values ('$v0','$v1','$v2','P','$ping')");
}
elsif ($check eq "snmp") {
$dbh->do("insert into services (description,service_ind,position,todo,variable) values ('$v0','$v1','$v2','S','$snmp')");
}
elsif ($check eq "konst") {
$dbh->do("insert into services (description,service_ind,position,todo,variable) values ('$v0','$v1','$v2','K','$konst')");
}
elsif ($check eq "calc") {
my $calc = "$calc1" . "$calc2" . "$calc3";
$dbh->do("insert into services (description,service_ind,position,todo,variable) values ('$v0','$v1','$v2','C','$calc')");
}
elsif ($check eq "result") {
my $result = "$result1" . "-" . "$result2";
$dbh->do("insert into services (description,service_ind,position,todo,variable) values ('$v0','$v1','$v2','R','$result')");
}
# $dbh->do("insert into alerts (description,alert_type,mail_from,mail_to,enterprise,specific,generic,destination,port,community,txt_msg) values ('$v0','$v1','$v2','$v3','$v4','$v6','$v5','$v7','$v8','$v9','$v10')");
print "</body>\n</html>\n";
+29
View File
@@ -0,0 +1,29 @@
#!/bin/perl
use Mysql;
use strict;
use CGI qw(:standard);
my $host = param('host');
my $service = param('service');
my $alert = param('alert');
my $check = param('check');
my $descr = param('descr');
my $active = param('active');
my $dbh = DBI->connect("DBI:mysql:database=c2;host=localhost",'root','', {RaiseError => 1});
my ($sth,$ref);
print header();
print "<html>\n";
print "<head>\n";
print "<style type='text/css'>\n";
print "select { font-size:8pt }\n";
#print "td { font-size:10pt }\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
$dbh->do("insert into todos (t_host,t_service,t_check,t_alert,t_active,t_description) values ('$host','$service','$check','$alert','$active','$descr')");
print "</body>\n</html>\n";
Binary file not shown.
+733
View 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;
}
+686
View File
@@ -0,0 +1,686 @@
#!/bin/perl
use strict;
use warnings;
use Mysql;
use Net::SNMP;
use Net::Ping;
use BER;
require 'SNMP_Session.pm';
use Data::Dumper;
our ($c2,$dbh);
main();
sub main {
my ($dbusr,$dbpwd,$dbcstr);
open INI, "<c2.ini" or die "Kann c2.ini nicht finden!\n";
while (<INI>) {
chomp;
($dbusr,$dbpwd,$dbcstr) = split /#/,$_;
}
close INI;
$dbh = DBI->connect("$dbcstr","$dbusr","$dbpwd", {RaiseError => 1});
while (1) {
read_database();
$c2->{'time'} = get_time();
$c2->{'date'} = get_date();
print "$c2->{'date'} $c2->{'time'}\n";
if(run_now()) {
foreach my $todo (keys %{$c2->{'todo'}}) {
get_service_value($todo);
}
#open TEST, ">TEST.txt";
#print TEST Dumper ($c2);
#close TEST;
create_web_from_template();
}
print "\n";
sleep ($c2->{'conf'}->{'check_intervall'});
}
}
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";
$sth = $dbh->prepare($sel);
$sth->execute;
while (my $ref = $sth->fetchrow_arrayref()) {
#print "#### $ref->[0]\n";
if ($ref->[0] ne "") {
$should_run=0;
}
}
$sel="select start_time from conf where active = '1'";
$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;
$sel="select stop_time from conf where active = '1'";
$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, ">index.html";
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'};
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'}} ) {
$sth = $dbh->prepare("select ind from todos where t_active='1'");
$sth->execute;
# print "$todo:";
while ($ref = $sth->fetchrow_arrayref()) {
my $found = 0;
if ($ref->[0] == $todo) {
$found=1;
}
unless ($found) {
delete $c2->{'todo'}->{$todo};
}
}
}
# 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;
}
+31
View File
@@ -0,0 +1,31 @@
<html>
<head>
<meta http-equiv='refresh' content='30; http://10.10.12.36/c2/'>
<style type='text/css'>
select { font-size:8pt }
td { font-size:10pt }
</style>
<title>c2_mysql</title>
</head>
<body name='seite'>
<a href="http://10.10.12.36/c2/admin/" target='konfig'>Konfiguration & Administration</a>
<table border="1">
<tr>
<td align="center" width="80"><b>Zeit</b></td>
<td align="center" width="100"><b>Host</b></td>
<td align="center" width="130"><b>IP</b></td>
<td align="center" width="150"><b>Dienst</b></td>
<td align="center" width="100"><b>Prüfung</b></td>
<td align="center" width="70"><b>Fehler?</b></td>
<td align="center" width="100"><b>ERGEBNIS</b></td>
</tr>
<tr bgcolor='ffffa0'><td align="center">14:12:14</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1471&edit1=Edit' target='konfig'>MQSICFSP001</a></td><td align="center">192.168.30.51</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">390069690</td></tr>
<tr bgcolor='a0ffa0'><td align="center">14:12:56</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1472&edit1=Edit' target='konfig'>MQSICFSP002</a></td><td align="center">192.168.30.52</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">391735171</td></tr>
<tr bgcolor='ffffa0'><td align="center">14:12:56</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1493&edit1=Edit' target='konfig'>SECUREGATEWAY</a></td><td align="center">192.169.1.133</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">No SNMP response</td></tr>
<tr bgcolor='a0ffa0'><td align="center">14:12:14</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1500&edit1=Edit' target='konfig'>nasicfsp004</a></td><td align="center">130.35.0.228</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">525112003</td></tr>
<tr bgcolor='ffffa0'><td align="center">14:12:35</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1527&edit1=Edit' target='konfig'>MFICFSP017</a></td><td align="center">10.10.30.189</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">No SNMP response</td></tr>
<tr bgcolor='a0ffa0'><td align="center">14:12:14</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=1528&edit1=Edit' target='konfig'>MFICFSP018</a></td><td align="center">10.10.10.118</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">532841004</td></tr>
<tr bgcolor='ffffa0'><td align="center">14:12:15</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_host.pl?edit=157&edit1=Edit' target='konfig'>ICFOM2</a></td><td align="center">10.10.10.56</td><td align="center">SYS_UPTIME</td><td align="center"><a href='http://10.10.12.36/c2/admin/edit_check.pl?edit=1&edit1=Edit' target='konfig'>> 2 Tage</a></td><td align="center"><font color='FF0000'><b>1</b></font></td><td align="center">278093674</td></tr>
</table>
</body>
</html>
+25
View File
@@ -0,0 +1,25 @@
<html>
<head>
[C2METAREFRESH]
<style type='text/css'>
select { font-size:8pt }
td { font-size:10pt }
</style>
<title>c2_mysql</title>
</head>
<body name='seite'>
<a href="[C2CONFIGURL]" target='konfig'>Konfiguration & Administration</a>
<table border="1">
<tr>
<td align="center" width="80"><b>Zeit</b></td>
<td align="center" width="100"><b>Host</b></td>
<td align="center" width="130"><b>IP</b></td>
<td align="center" width="150"><b>Dienst</b></td>
<td align="center" width="100"><b>Prüfung</b></td>
<td align="center" width="70"><b>Fehler?</b></td>
<td align="center" width="100"><b>ERGEBNIS</b></td>
</tr>
[C2REPEAT] DisplayError <tr><td align="center">[C2SERVICETIME]</td><td align="center">[C2HOSTNAME]</td><td align="center">[C2HOSTIP]</td><td align="center">[C2SERVICENAME]</td><td align="center">[C2CHECKDESCRIPTION]</td><td align="center">[C2ERROR]</td><td align="center">[C2RESULT]</td></tr>
</table>
</body>
</html>