-
Notifications
You must be signed in to change notification settings - Fork 2
/
BayourCOM_SNMP.pm
158 lines (125 loc) · 3.37 KB
/
BayourCOM_SNMP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# {{{ $Id: BayourCOM_SNMP.pm,v 1.7 2007-12-31 11:30:00 turbo Exp $
# Common functions used by Bayour.COM SNMP modules.
#
# Copyright 2005 Turbo Fredriksson <[email protected]>.
# This software is distributed under GPL v2.
# }}}
package BayourCOM_SNMP;
use POSIX qw(strftime);
require Exporter;
use vars qw(@EXPORT @ISA %CFG);
%CFG = ();
@ISA = qw(Exporter);
@EXPORT = qw(help debug no_value check_val get_config output_extra_debugging %CFG);
our $log_opened = 0;
# ----- INTERNAL
# {{{ Find the current date and time
# Returns a string something like: '10/8-96 16:27'
sub get_timestring {
my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
return POSIX::strftime("20%y-%m-%d %H:%M:%S",
$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
}
# }}}
# {{{ Open logfile for debugging
sub open_log {
die("DEBUG_FILE not set in config file!\n") if(!$CFG{'DEBUG_FILE'});
if(!open(LOG, ">> ".$CFG{'DEBUG_FILE'})) {
printf(STDERR "Can't open logfile '".$CFG{'DEBUG_FILE'}."', $!\n") if($CFG{'DEBUG'});
return 0;
} else {
return 1;
}
}
# }}}
# ----- EXTERNAL
# {{{ Show usage
sub help {
my $name = `basename $0`; chomp($name);
printf("Usage: $name [option] [oid]\n");
printf("Options: --debug|-d Run in debug mode\n");
printf(" --all|-a Get all information\n");
printf(" -n Get next OID ('oid' required)\n");
printf(" -g Get specified OID ('oid' required)\n");
exit 1 if($CFG{'DEBUG'});
}
# }}}
# {{{ Log output
sub debug {
my $stdout = shift;
my $string = shift;
# Open logfile if debugging OR running from snmpd.
if($stdout) {
print $string;
} elsif($CFG{'DEBUG'}) {
if(&open_log()) {
open(STDERR, ">LOG") if(($CFG{'DEBUG'} <= 2) || $ENV{'MIBDIRS'});
print LOG get_timestring()," " if($CFG{'DEBUG'} > 2);
print LOG $string;
LOG->autoflush(1);
close(LOG);
}
}
}
# }}}
# {{{ Return 'no such value'
sub no_value {
debug(0, "=> No value in this object - exiting!\n") if($CFG{'DEBUG'} > 1);
debug(1, "NONE\n");
debug(0, "\n") if($CFG{'DEBUG'} > 1);
}
# }}}
# {{{ check_val()
sub check_val {
my $value = shift;
if(defined($value)) {
return(1);
} else {
return(0);
}
}
# }}}
# {{{ Load configuration file
sub get_config {
my $cfg_file = shift;
my $option = shift;
my($line, $key, $value);
my(%CFG);
$option = 0 if(!defined($option));
if(-e $cfg_file) {
open(CFG, "< ".$cfg_file) || die("Can't open $cfg_file, $!\n");
while(!eof(CFG)) {
$line = <CFG>; chomp($line);
next if($line !~ /^[A-Z]/);
($key, $value) = split('=', $line);
if(!$option) {
# Get all options
$CFG{$key} = $value;
} elsif($option eq $key) {
# Return only this option
return $value;
}
}
close(CFG);
}
$CFG{'DEBUG'} = 0 if(!defined($CFG{'DEBUG'}));
$CFG{'IGNORE_INDEX'} = 0 if(!defined($CFG{'IGNORE_INDEX'}));
# A debug value from the environment overrides!
$CFG{'DEBUG'} = $ENV{'DEBUG_BIND9'} if(defined($ENV{'DEBUG_BIND9'}));
return(%CFG);
}
# }}}
# {{{ Output some extra debugging
sub output_extra_debugging {
my @tmp = @_;
my $string = "=> ";
for(my $i=0; defined($tmp[$i]); $i++) {
$string .= "tmp[$i]=".$tmp[$i];
$string .= ", " if(defined($tmp[$i+1]));
}
$string .= "\n";
debug(0, $string);
}
# }}} # Extra debugging
1;
__END__