Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use NetSNMP::agent (':all');
use NetSNMP::ASN (':all');
use Time::Local;
my $OID = ".1.3.6.1.4.1.57397.1"; # clustercontrolMIB
sub myhandler {
my ($handler, $registration_info, $request_info, $requests) = @_;
my $request;
for($request = $requests; $request; $request = $request->next()) {
my $oid = $request->getOID();
my $clusterId = 23; # cluster ID that you want to monitor
my $totalAlarm = `/bin/s9s alarms --list --cluster-id=$clusterId --batch | wc -l`;
my $criticalAlarm = `/bin/s9s alarms --list --cluster-id=$clusterId --batch | grep CRITICAL | wc -l`;
my $warningAlarm = `/bin/s9s alarms --list --cluster-id=$clusterId --batch | grep WARNING | wc -l`;
if ($request_info->getMode() == MODE_GET) {
# ... generally, you would calculate value from oid
if ($oid == new NetSNMP::OID($OID . ".1.1.1")) {
$request->setValue(ASN_INTEGER, $totalAlarm);
}
elsif ($oid == new NetSNMP::OID($OID . ".1.1.2")) {
$request->setValue(ASN_INTEGER, $criticalAlarm);
}
elsif ($oid == new NetSNMP::OID($OID . ".1.1.3")) {
$request->setValue(ASN_INTEGER, $warningAlarm);
}
elsif ($oid == new NetSNMP::OID($OID . ".1.1.4")) {
$request->setValue(ASN_INTEGER, $clusterId);
}
} elsif ($request_info->getMode() == MODE_GETNEXT) {
if ($oid == new NetSNMP::OID($OID . ".1.1.1")) {
$request->setOID($OID . ".1.1.2");
$request->setValue(ASN_INTEGER, $criticalAlarm);
}
elsif ($oid == new NetSNMP::OID($OID . ".1.1.2")) {
$request->setOID($OID . ".1.1.3");
$request->setValue(ASN_INTEGER, $warningAlarm);
}
elsif ($oid == new NetSNMP::OID($OID . ".1.1.3")) {
$request->setOID($OID . ".1.1.4");
$request->setValue(ASN_INTEGER, $clusterId);
}
elsif ($oid < new NetSNMP::OID($OID . ".1.1.1")) {
$request->setOID($OID . ".1.1.1");
$request->setValue(ASN_INTEGER, $totalAlarm);
}
} elsif ($request_info->getMode() == MODE_SET_RESERVE1) {
if ($oid != new NetSNMP::OID($OID . "1.1")) { # do error checking here
$request->setError($request_info, SNMP_ERR_NOSUCHNAME);
}
} elsif ($request_info->getMode() == MODE_SET_ACTION) {
# ... (or use the value)
$value = $request->getValue();
}
}
}
my $agent = new NetSNMP::agent(
'Name' => "clustercontrol_snmp_agent",
'AgentX' => 1
);
$agent->register("clustercontrol_snmp_agent", ".1.3.6.1.4.1.57397.1",
\&myhandler);
my $running = 1;
while($running) {
$agent->agent_check_and_process(1);
}
$agent->shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment