-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.nod.snmptools.pm
More file actions
117 lines (107 loc) · 5.45 KB
/
create.nod.snmptools.pm
File metadata and controls
117 lines (107 loc) · 5.45 KB
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
#<ACTION> file=>'nod/snmptools.pm',hook=>'new'
# -------------------------- NoDeny --------------------------
# Created by Redmen for NoDeny (https://nodeny.com.ua)
# https://forum.nodeny.com.ua/index.php?action=profile;u=1139
# https://t.me/MrMethod
# ------------------------------------------------------------
# Info: SNMP tools for PON monitoring
# NoDeny: rev. 718
# Update: 2025.11.01
# ------------------------------------------------------------
package nod::snmptools;
use strict;
use Debug;
my %env_path = map{ $_ => 1 } split /\:/, $ENV{'PATH'}.':/usr/local/bin:/usr/bin:/bin';
$ENV{'PATH'} = join(':', keys %env_path);
sub new {
my ($class, $olt) = @_;
my $obj = {};
$obj->{host} = $olt->{ip};
$obj->{port} = int($olt->{cfg}{snmp_port} // 161);
$obj->{version} = $olt->{cfg}{snmp_version} // '2c';
$obj->{timeout} = int($olt->{cfg}{snmp_timeout} // 10);
$obj->{retries} = int($olt->{cfg}{snmp_retries} // 10);
$obj->{repeaters} = int($olt->{cfg}{snmp_repeaters} // 0);
$obj->{community_ro} = $olt->{cfg}{snmp_community_ro} || 'public';
$obj->{community_rw} = $olt->{cfg}{snmp_community_rw} || 'private';
$obj->{version} = $obj->{version} =~ /1|3/ ? $obj->{version} : '2c';
bless $obj, $class;
return $obj;
}
########################################################################################
# Performs an SNMP GET and returns the response as a Hashref.
########################################################################################
sub get {
my ($self, $oids) = @_;
my %resp = ();
foreach my $oid (keys %{$oids}) {
# debug "$oid => $oids->{$oid}";
debug "snmpget -r $self->{retries} -t $self->{timeout} -v $self->{version} -On -Oe -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid}";
my $output = `snmpget -r $self->{retries} -t $self->{timeout} -v $self->{version} -On -Oe -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid} 2>/dev/null`;
if ($output =~ /\s*(\.?[\d\.]*)\s*\=\s*([\w\-]*)\:\s*((.*\s?)+)\s*$/) {
my ($index, $value) = ($1, $3);
$value =~ s/^\s*|\s*$//gm;
$resp{$oid} = $value;
}
}
# debug 'pre', \%resp;
return \%resp;
}
########################################################################################
# Performs an SNMP SET and returns the response as a Hashref.
########################################################################################
sub set {
my ($self, $oids) = @_;
return { error => 'Invalid OIDs array' } unless ref($oids) eq 'ARRAY' && @$oids;
my $cmd = "snmpset -t $self->{timeout} -v $self->{version} -c $self->{community_rw} -On -Ir $self->{host}:$self->{port}";
foreach my $oid (@$oids) {
return { error => 'Each OID must have oid, type, and value' } unless (ref($oid) eq 'HASH' && exists $oid->{oid} && exists $oid->{type} && exists $oid->{value});
$cmd .= ' ' . $oid->{oid} . ' ' . $oid->{type} . ' ' . $oid->{value};
}
my $output = `$cmd 2>/dev/null`;
debug 'pre', $output;
return $? == 0 ? { success => 1, output => $output } : { error => "Command failed: $cmd" };
}
########################################################################################
# Performs an SNMP WALK and returns the response as a Hashref.
########################################################################################
sub walk {
my ($self, $oids) = @_;
my %resp = ();
foreach my $oid (keys %{$oids}) {
#debug "$oid => $oids->{$oid}\n";
my @output = ();
if (int($self->{repeaters}) == 1) {
debug "snmpbulkwalk -On -Oe -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid}";
@output = `snmpbulkwalk -On -Oe -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid} 2>/dev/null`;
} elsif (int($self->{repeaters}) > 1) {
debug "snmpbulkwalk -On -Oe -Cn1 -Cr$self->{repeaters} -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid}";
@output = `snmpbulkwalk -On -Oe -Cn1 -Cr$self->{repeaters} -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid} 2>/dev/null`;
} else {
debug "snmpwalk -On -Oe -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid}";
@output = `snmpwalk -On -Oe -r $self->{retries} -t $self->{timeout} -v $self->{version} -c $self->{community_ro} $self->{host}:$self->{port} $oids->{$oid} 2>/dev/null`;
}
$resp{$oid} = to_hash($oids->{$oid}, \@output);
}
# debug 'pre', \%resp;
return \%resp;
}
########################################################################################
# Helper function to convert SNMP walk output to a Hashref.
########################################################################################
sub to_hash {
my $oid = shift;
my $output = shift;
my %values;
foreach my $line (@{$output}) {
chomp $line;
$line =~ s/$oid//;
if ($line =~ /^\s*\.*([\d\.]*)\s*\=\s*[\w\-]*\:\s*(.*)\s*$/) {
my ($index, $value) = ($1, $2);
$value =~ s/^\s*|\s*$//gm;
$values{$index} = $value;
}
}
return \%values;
}
1;