-
Notifications
You must be signed in to change notification settings - Fork 1
/
SnmpCommunities.pm
177 lines (153 loc) · 6.19 KB
/
SnmpCommunities.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package SnmpCommunities;
use strict;
use Log::Log4perl qw(get_logger);
use Constants;
use PetesUtils;
use SwitchUtils;
sub ReadCommunityFromCacheFile ($) {
my $DeviceName = shift; # passed in
my $logger = get_logger('log4');
$logger->info("ReadCommunityFromCache: called");
my $RetVal = ''; # false
my $CommunityFileName = File::Spec->catfile($Constants::CommunityDirectory, $DeviceName);
if (-r $CommunityFileName) {
$logger->debug("found cached SNMP community file for $DeviceName");
open COMMUNITY, "<$CommunityFileName" or
die "couldn't open cached community file for $DeviceName: $!";
$RetVal = <COMMUNITY>;
close COMMUNITY;
} else {
$logger->info("no entry for $DeviceName yet");
}
$RetVal = '' if !defined $RetVal;
# $logger->info("ReadCommunityFromCache: returning \"$RetVal\"");
$logger->info("ReadCommunityFromCache: returning");
return $RetVal;
}
sub WriteCommunityToCacheFile ($$) {
my $DeviceName = shift; # passed in
my $Community = shift; # passed in
my $logger = get_logger('log4');
# If the user has configured the $StateFileDirectory to be the same
# as the destination directory, it's a security hole. We don't want
# to write a bunch of SNMP community strings into the destination
# directory, where they can be browsed by web users. So just
# quietly return if this is the case.
return if $ThisSite::StateFileDirectory eq $ThisSite::DestinationDirectory;
# Create the directory if it doesn't already exist
if (!-d $Constants::CommunityDirectory) {
mkdir $Constants::CommunityDirectory or do {
$logger->fatal("Couldn't create $Constants::CommunityDirectory, $!");
exit;
};
$logger->debug("created $Constants::CommunityDirectory");
}
my $CommunityFileName = File::Spec->catfile($Constants::CommunityDirectory, $DeviceName);
$logger->debug("opening file CommunityFileName = \"$CommunityFileName\" for writing");
open COMMUNITYFILE, ">$CommunityFileName" or
die "couldn't open cached community string file $CommunityFileName: $!";
print COMMUNITYFILE $Community;
close COMMUNITYFILE;
SwitchUtils::AllowOnlyOwnerToReadFile $CommunityFileName;
$logger->debug("returning");
}
my @SnmpCommunities;
#
# Given a list of community strings, return the optimized list.
# Optimized means that if there's a cached community string for the
# device, it appears first in the list.
#
sub PutCachedCommunityFirst($) {
my $DeviceName = shift;
my $logger = get_logger('log4');
$logger->debug("called");
my $cachedCommunity = ReadCommunityFromCacheFile $DeviceName;
my @ReorderedCommunities = @SnmpCommunities;
if ($cachedCommunity) {
for (my $i=0; $i<=$#ReorderedCommunities; $i++) {
if ($ReorderedCommunities[$i] eq $cachedCommunity) {
# move the entry to the front of the list
unshift @ReorderedCommunities, splice @ReorderedCommunities, $i, 1;
last;
}
}
}
$logger->debug("returning");
return @ReorderedCommunities;
}
#
# The SNMP community string(s) to be used to talk to switches are
# defined by the value of $ThisSite::Community. See ThisSite.pm for
# an explanation of the possible values. This function takes care of
# the details, and returns an array of possible community strings to
# be used when talking to the switches.
#
sub initialize () {
my $logger = get_logger('log1');
$logger->debug("called");
if ($ThisSite::CmstrFile) { # if there's a list of communities to try
if (($ThisSite::GetSnmpCommunitiesFromHpOpenView) and
($ThisSite::OpenViewHost ne 'localhost')) {
$logger->info("Getting SNMP communities via ssh from file $ThisSite::CmstrFile on $ThisSite::OpenViewHost...");
open COMMSTRFILE, "/usr/bin/ssh $ThisSite::SshKeyOption $ThisSite::OpenViewHost cat $ThisSite::CmstrFile |" or do {
$logger->fatal("Couldn't read file $ThisSite::CmstrFile from host $ThisSite::OpenViewHost via ssh, $!.\n" .
"The file name came from \$CmstrFile in ThisSite.pm, see comments in ThisSite.pm");
exit;
};
} else {
$logger->info("reading SNMP communities from file $ThisSite::CmstrFile on local host...");
open COMMSTRFILE, "<$ThisSite::CmstrFile" or do {
$logger->fatal("Couldn't open $ThisSite::CmstrFile for reading, $!.\n" .
"The file name came from \$CmstrFile in ThisSite.pm, see comments in ThisSite.pm");
exit;
};
}
if (eof COMMSTRFILE) {
$logger->fatal("Couldn't read $ThisSite::CmstrFile,\n" .
"The file name came from \$CmstrFile in ThisSite.pm, see the comments in ThisSite.pm");
exit;
}
my %Cstrs; # used to detect duplicates
while (<COMMSTRFILE>) {
next if /^#/; # skip comment lines
/^"([^"]+)"/; # pull out the community string
my $cstr = $1;
if (defined $1) {
next if exists $Cstrs{$cstr}; # skip duplicates
$Cstrs{$cstr}++;
push @SnmpCommunities, $cstr;
}
}
close COMMSTRFILE;
} else { # not using a file
push @SnmpCommunities, $ThisSite::Community; # so it's just a simple single community string
}
if ($#SnmpCommunities == -1) {
$logger->fatal("No SNMP community strings defined. Something about the value of\n" .
"\$CmstrFile and/or \$Community in ThisSite.pm isn't right.");
exit;
}
# foreach my $Comm (@SnmpCommunities) {
# $logger->debug("returning, candidate SNMP community string: \"$Comm\"");
# }
my $NbrStrings = $#SnmpCommunities+1;
$logger->info("got $NbrStrings SNMP community strings");
$logger->debug("returning");
}
sub GetCommunities ($) {
my $logger = get_logger('log3');
my $DeviceName = shift;
$logger->debug("called, DeviceName = \"$DeviceName\"");
return PutCachedCommunityFirst($DeviceName);
}
# To unit-test this module, uncomment the following lines and then
# run it with "perl SnmpCommunities.pm"
# my $opt_d = 1;
# my $opt_i = 0;
# my $opt_w = 0;
# PetesUtils::InitializeLogging(0, $opt_d, $opt_i, $opt_w, $Constants::MAX_DEBUGGING_MESSAGE_DEPTH);
# initialize();
# foreach my $SnmpCommunity (getCommunities()) {
# print "Community = $SnmpCommunity\n";
# }
1;