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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#!@@PERL@@
# -*- perl -*-
=head1 NAME
cmc_tc_sensor_ - Wildcard plugin for monitoring CMC temperature sensors via SNMP
=head1 CONFIGURATION
This is a wildcard plugin. The plugin link name prefix (after the final "_")
should be a hostname to contact via SNMP.
This plugin does not use configuration environment variables.
=head1 AUTHOR
Unknown author
=head1 LICENSE
GPLv2
=head1 BUGS
Possibly misnamed. Candidate to be snmp__cmctcsensor_
=head1 MAGIC MARKERS
#%# family=contrib
#%# capabilities=suggest
=cut
use strict;
use warnings;
use FindBin qw ($Bin $Script);
use Net::SNMP;
use Data::Dumper;
use vars qw ($configure $suggest $sensor_unit $sensor_index %sensor);
# Quit if run without specified sensors
unless ($0 =~ /_\d+_\d+$/ || ($ARGV[0] && $ARGV[0] eq "suggest")) {
print "Need sensor spesification symlinked in file name.\n";
print "Hint: $0 suggest\n";
exit 1;
}
# To configure
if ($ARGV[0] && $ARGV[0] eq "config") {
$configure = 1;
}
# 'suggest' creates proper symlinks.
if ($ARGV[0] && $ARGV[0] eq "suggest") {
$suggest = 1;
}
# Find variable
if ($0 =~ /^.*\_(\d+)\_(\d+)$/) {
$sensor_unit = $1;
$sensor_index = $2;
}
my $host = ($ENV{'hostname'} || '127.0.0.1');
my $comm = ($ENV{'communitystring'} || 'public');
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $comm,
-port => 161,
-version => 'snmpv1',
);
if (!defined ($session)) {
printf ("ERROR: %s.\n", $error);
exit 1;
} else {
if (($configure && $sensor_unit && $sensor_index) || $suggest) {
my %properties = (
1 => 'index',
2 => 'type',
3 => 'desc',
4 => 'status',
5 => 'value',
6 => 'high',
7 => 'low',
8 => 'warn',
);
# TODO: Fill out more from MIB
my %sensor_types = (
1 => 'not available',
10 => 'temperature',
);
if ($configure) {
# .1.3.6.1.4.1.2606.4.2.3.5.2.1.x.1
# unit -^ ^ ^- index
# `- property
my $baseoid = "1.3.6.1.4.1.2606.4.2." . (2 + $sensor_unit) . ".5.2.1";
if (defined (my $response = $session->get_table ($baseoid))) {
while (my ($oid, $value) = each (%$response)) {
my $prop_id = undef;
if ($oid =~ /(?:\d+\.){13}(\d+)(?:\.\d+){1}/) {
$prop_id = $1;
}
(my $j = $oid) =~ s/^.*\.(\d)$/$1/;
if ($j == $sensor_index) {
my $prop = $properties{$prop_id};
# print "Sensor: $sensor_unit \t Prop: $prop \t Index: $sensor_index \t Value: $value\n";
$sensor{$sensor_unit}->{$sensor_index}->{$prop} = $value;
}
}
my $sensor_name = "sensor_" . $sensor_unit . "_" . $sensor_index;
my $type = $sensor{$sensor_unit}->{$sensor_index}->{'type'};
print "graph_title Sensor ${sensor_unit}\/${sensor_index}\n";
print "graph_args --base 1000 -l 0\n";
print "graph_category Environment\n";
if ($sensor_types{$type} eq "temperature") {
# Find thermometer scale (Celsius or Fahrenheit);
my $temp_scale_oid = "1.3.6.1.4.1.2606.4.3.1.1.0";
my %temp_scales = (
1 => "Celsius",
2 => "Fahrenheit",
);
if (defined (my $response = $session->get_request ($temp_scale_oid))) {
my (undef, $scale) = each (%$response);
print "graph_label Degrees $temp_scales{$scale}\n";
print "graph_vlabel Degrees $temp_scales{$scale}\n";
}
}
print "$sensor_name\.label " . $sensor{$sensor_unit}->{$sensor_index}->{'desc'} . "\n";
print "$sensor_name\.warning " . $sensor{$sensor_unit}->{$sensor_index}->{'low'} . ":" . $sensor{$sensor_unit}->{$sensor_index}->{'warn'} . "\n";
print "$sensor_name\.critical " . $sensor{$sensor_unit}->{$sensor_index}->{'low'} . ":" . $sensor{$sensor_unit}->{$sensor_index}->{'high'} . "\n";
}
} elsif ($suggest) {
# Find number of connected CMC units
my $num_units_oid = '1.3.6.1.4.1.2606.4.2.2.0';
my $num_units = $session->get_request ($num_units_oid)->{$num_units_oid};
# .1.3.6.1.4.1.2606.4.2.3.5.2.1.1.1
# ^- 2 + $unit_no
for (my $i = 1; $i <= $num_units; $i++) {
# Walk each table
my $baseoid = "1.3.6.1.4.1.2606.4.2." . (2 + $i) . ".5.2.1";
# Find properties and values
# We only traverse the OID tree once per sensor,
# to reduce number of SNMP GET requests.
if (defined (my $response = $session->get_table ($baseoid))) {
while (my ($oid, $value) = each (%$response)) {
my $prop_id = undef;
if ($oid =~ /(?:\d+\.){13}(\d+)(?:\.\d+){1}/) {
$prop_id = $1;
}
my $prop = $properties{$prop_id};
(my $j = $oid) =~ s/^(?:\d+\.)*(\d+)$/$1/g;
# print "Sensor: $i \t Prop: $prop \t Index: $j \t Value: $value\n";
$sensor{$i}->{$j}->{$prop} = $value;
}
} else {
print $session->error, "\n";;
}
}
# Now, traverse the sensor tree(s)
while (my ($key, $val) = each (%sensor)) {
while (my ($key2, $val2) = each (%$val)) {
# Check if sensor is available (value == 4)
if ($sensor{$key}->{$key2}->{'status'} == 4) {
my $filename = "/etc/munin/plugins/cmc-tc_sensor_" . $key . "_" . $key2;
`ln -s $Bin\/$Script $filename`;
}
}
}
}
} else {
if (defined ($session)) {
# Go directly to the OID we want
my $oid = "1.3.6.1.4.1.2606.4.2." . (2 + $sensor_unit) . ".5.2.1.5." . $sensor_index;
my $value = $session->get_request ($oid)->{$oid};
# Show values
print "sensor_" . $sensor_unit . "_" . $sensor_index . ".value $value\n";
}
}
$session->close();
}
exit 0;
|