Centreon atualizado para versão 2.1.12

Foi liberada versão 2.1.12 do Centreon.
Poderá verificar o changelog em: http://www.centreon.com/Development/changelog-2x.html

O procedimento para upgrade é o mesmo já publicado anteriormente. Poderá ler aqui.
Lembre-se (obviamente) de apontar para a nova URL de download (http://download.centreon.com/index.php?id=149) e rodar os comandos como 2.1.12!!

Upgrade da versão do Centreon para 2.1.11

Para fazer o upgrade da versão do Centreon já instalado basta seguir os passos abaixo:

cd /usr/local/src
wget http://download.centreon.com/index.php?id=148
tar -xzf centreon-2.1.11.tar.gz
cd centreon-2.1.11
./install.sh -u /etc/centreon
Seguir as instruções do script (normalmente você irá responder Y para tudo, mas é bom ler a pergunta né!!).
Para finalizar o upgrade voce precisará acessar a interface Web e seguir por mais 3 ou 4 telas.
Simples assim!

Monitoring Quescom Gateways with Nagios

Como não achei nenhum script que permita o monitoramento de Gateways Quescom através do Nagios portanto resolvi escrever um.
Abaixo está o resultado deste trabalho. Por enquanto o script permite fazer 3 tipos de checagens (utilizar o parametro -s para informar a checagem desejada):
  1. gsmsignal - Irá verificar a potencia do sinal GSM em cada Chip instalado (utilizar o parametro -t para informar o chip desejado)
  2. gsminuse - Irá verificar quantos dos 4 canais GSM estão sendo utilizados para chamadas de saida (Used Out) ou entrada (Used In)
  3. voipinusetotal - Irá verificar quantos canais VoIP (SIP/H.323) estão sendo utilizados para entrada ou saida (utilizar o parametro -t para informar o Foreign Gatekeeper desejado, normalmente o primeiro criado será 1 e assim por diante)

#!/usr/bin/perl
# Verifica Informações do Gateway Quescom

use Net::SNMP;
use Getopt::Std;

$script = "check_snmp_quescom";
$script_version = "1.0.0";

$metric = 1;

$ipaddress = "192.168.1.1"; # default IP address, if none supplied
$version = "1"; # SNMP version
$timeout = 2; # SNMP query timeout
$status = 0;
$returnstring = "";
$community = "public"; # Default community string

$oid_comms = ".1.3.6.1.4.1.9622.1.1.4.0";
$oid_gsm_sinal_1 = ".1.3.6.1.4.1.9622.11.2.8.1.7.0.0";
$oid_gsm_sinal_2 = ".1.3.6.1.4.1.9622.11.2.8.1.7.0.1";
$oid_gsm_sinal_3 = ".1.3.6.1.4.1.9622.11.2.8.1.7.0.2";
$oid_gsm_sinal_4 = ".1.3.6.1.4.1.9622.11.2.8.1.7.0.3";
$oid_gsm_status_1 = ".1.3.6.1.4.1.9622.11.2.8.1.5.0.0";
$oid_gsm_status_2 = ".1.3.6.1.4.1.9622.11.2.8.1.5.0.1";
$oid_gsm_status_3 = ".1.3.6.1.4.1.9622.11.2.8.1.5.0.2";
$oid_gsm_status_4 = ".1.3.6.1.4.1.9622.11.2.8.1.5.0.3";
$oid_voip_usedsessions_1 = ".1.3.6.1.4.1.9622.11.3.2.1.6.0.0";
$oid_voip_usedsessions_2 = ".1.3.6.1.4.1.9622.11.3.2.1.6.0.1";
$oid_voip_usedsessions_3 = ".1.3.6.1.4.1.9622.11.3.2.1.6.0.2";
$oid_voip_usedsessions_4 = ".1.3.6.1.4.1.9622.11.3.2.1.6.0.3";
$oid_voip_name_1 = ".1.3.6.1.4.1.9622.11.3.2.1.2.0.0";
$oid_voip_name_2 = ".1.3.6.1.4.1.9622.11.3.2.1.2.0.1";
$oid_voip_name_3 = ".1.3.6.1.4.1.9622.11.3.2.1.2.0.2";
$oid_voip_name_4 = ".1.3.6.1.4.1.9622.11.3.2.1.2.0.3";

# Do we have enough information?
if (@ARGV < 1) {
print "Too few arguments\n";
usage();
}

getopts("h:H:S:C:w:c:t:");
if ($opt_h){
usage();
exit(0);
}
if ($opt_H){
$hostname = $opt_H;
}
else {
print "No hostname (H) specified\n";
usage();
}
if ($opt_S){
$service = $opt_S;
}
else {
print "No Service to Check specified\n";
usage();
}
if ($opt_C){
$community = $opt_C;
}
if ($opt_c){
$critical = $opt_c;
}
else {
print "No critical value specified\n";
usage();
}
if ($opt_w){
$warning = $opt_w;
}
else {
print "No warning value specified\n";
usage();
}

# Create the SNMP session for Host
my ($s1, $e1) = Net::SNMP->session(
-community => $community,
-hostname => $hostname,
-version => $version,
-timeout => $timeout,
);

if ($service eq "gsmsignal"){
if ($opt_t == 1){
$gsmtrunk = $oid_gsm_sinal_1;
$gsmstatus = $oid_gsm_status_1;
}
elsif ($opt_t == 2){
$gsmtrunk = $oid_gsm_sinal_2;
$gsmstatus = $oid_gsm_status_2;
}
elsif ($opt_t == 3){
$gsmtrunk = $oid_gsm_sinal_3;
$gsmstatus = $oid_gsm_status_3;
}
elsif ($opt_t == 4){
$gsmtrunk = $oid_gsm_sinal_4;
$gsmstatus = $oid_gsm_status_4;
}
else {
print "No or Invalid GSM Trunk to check specified\n $gsmtrunkoption";
usage();
}
gsmsignal();
verifygsmsignal();
}

if ($service eq "voipinusetotal"){
if ($opt_t == 1){
$voipusedsessions = $oid_voip_usedsessions_1;
$voipname = $oid_voip_name_1;
}
elsif ($opt_t == 2){
$voipusedsessions = $oid_voip_usedsessions_2;
$voipname = $oid_voip_name_2;
}
elsif ($opt_t == 3){
$voipusedsessions = $oid_voip_usedsessions_3;
$voipname = $oid_voip_name_3;
}
elsif ($opt_t == 4){
$voipusedsessions = $oid_voip_usedsessions_4;
$voipname = $oid_voip_name_4;
}
else {
print "No or Invalid Foreign Gatekeeper to check specified\n $gsmtrunkoption";
usage();
}
voipinusetotal();
verifyvoipinusetotal();
}


if ($service eq "gsminuse"){
gsminuse();
verifygsminuse();
}

# Close the session for Host
$s1->close();

if ($returnstring eq ""){
$status = 3;
}

if ($status == 0){
print "Status is OK $returnstring\n";
# print "$returnstring\n";
}
elsif ($status == 1){
print "Status is a WARNING $returnstring\n";
}
elsif ($status == 2){
print "Status is CRITICAL $returnstring\n";
}
elsif ($status == 3){
print "Status is UNKNOWN $returnstring\n";
}
else{
print "Problem with plugin. No response from SNMP agent.\n";
}

exit $status;


####################################################################
# This is where we gather data via SNMP and return results #
####################################################################

sub gsmsignal {

#######################################################
if (!defined($s1->get_request($oid_comms))) {
}
foreach ($s1->var_bind_names()) {
$temp = $s1->var_bind_list()->{$_};
}
if ($temp eq "SmartIAD"){
}
else {
append("- SNMP agent not communicating with Quescom Gateway");
$statushost = 3;
return 1;
}
#######################################################
if (!defined($s1->get_request($gsmstatus))) {
}
foreach ($s1->var_bind_names()) {
$gsmstatus_result = $s1->var_bind_list()->{$_};
}
#######################################################
if (!defined($s1->get_request($gsmtrunk))) {
}
foreach ($s1->var_bind_names()) {
$gsmtrunk_signal = $s1->var_bind_list()->{$_};
}
}
sub verifygsmsignal {
$issue = "";

if ($gsmtrunk_signal <= $warning) {
$status = 1;
}
if ($gsmtrunk_signal <= $critical) {
$status = 2;
}
if ($gsmstatus_result eq "Inactive") {
$status = 2;
}
if ($statushost == 3) {
$status = 3;
}

my $perfdata="| 'signal'=$gsmtrunk_signal;$warning;$critical;";

if ($status == 0){
$temp = sprintf "$issue - Signal GSM Channel $opt_t ($gsmstatus_result): $gsmtrunk_signal dBm $perfdata";
}
else {
$temp = sprintf "$issue - Signal GSM Channel $opt_t ($gsmstatus_result): $gsmtrunk_signal dBm $perfdata";
}
if ($gsmstatus_result eq "Inactive"){
$temp = sprintf "$issue - Signal GSM Channel $opt_t ($gsmstatus_result): Sem Sinal da Operadora!! $perfdata";
}

append($temp);
}

sub voipinusetotal {

#######################################################
if (!defined($s1->get_request($oid_comms))) {
}
foreach ($s1->var_bind_names()) {
$temp = $s1->var_bind_list()->{$_};
}
if ($temp eq "SmartIAD"){
}
else {
append("- SNMP agent not communicating with Quescom Gateway");
$statushost = 3;
return 1;
}
#######################################################
if (!defined($s1->get_request($voipusedsessions))) {
}
foreach ($s1->var_bind_names()) {
$voipusedsessions_result = $s1->var_bind_list()->{$_};
}
#######################################################
if (!defined($s1->get_request($voipname))) {
}
foreach ($s1->var_bind_names()) {
$voipname_result = $s1->var_bind_list()->{$_};
}
}
sub verifyvoipinusetotal {
$issue = "";

if ($voipusedsessions_result >= $warning) {
$status = 1;
}
if ($voipusedsessions_result >= $critical) {
$status = 2;
}

if ($statushost == 3) {
$status = 3;
}

my $perfdata="| 'voipinuse'=$voipusedsessions_result;$warning;$critical;";

if ($status == 0){
$temp = sprintf "$issue - VoIP Channels In Use for $voipname_result: $voipusedsessions_result $perfdata";
}
else {
$temp = sprintf "$issue - VoIP Channels In Use for $voipname_result: $voipusedsessions_result $perfdata";
}
append($temp);
}

sub gsminuse {

#######################################################
if (!defined($s1->get_request($oid_comms))) {
}
foreach ($s1->var_bind_names()) {
$temp = $s1->var_bind_list()->{$_};
}
if ($temp eq "SmartIAD"){
}
else {
append("- SNMP agent not communicating with Quescom Gateway");
$statushost = 3;
return 1;
}

#######################################################
if (!defined($s1->get_request($oid_gsm_status_1))) {
}
foreach ($s1->var_bind_names()) {
$gsm_status_1 = $s1->var_bind_list()->{$_};
}
#######################################################
if (!defined($s1->get_request($oid_gsm_status_2))) {
}
foreach ($s1->var_bind_names()) {
$gsm_status_2 = $s1->var_bind_list()->{$_};
}
#######################################################
if (!defined($s1->get_request($oid_gsm_status_3))) {
}
foreach ($s1->var_bind_names()) {
$gsm_status_3 = $s1->var_bind_list()->{$_};
}
#######################################################
if (!defined($s1->get_request($oid_gsm_status_4))) {
}
foreach ($s1->var_bind_names()) {
$gsm_status_4 = $s1->var_bind_list()->{$_};
}
}
sub verifygsminuse {
$issue = "";
$gsm_inuse_1 = 0;
$gsm_inuse_2 = 0;
$gsm_inuse_3 = 0;
$gsm_inuse_4 = 0;

if (($gsm_status_1 eq "Used In") || ($gsm_status_1 eq "Used Out")) {
$gsm_inuse_1 = 1;
}
if (($gsm_status_2 eq "Used In") || ($gsm_status_2 eq "Used Out")) {
$gsm_inuse_2 = 1;
}
if (($gsm_status_3 eq "Used In") || ($gsm_status_3 eq "Used Out")) {
$gsm_inuse_3 = 1;
}
if (($gsm_status_4 eq "Used In") || ($gsm_status_4 eq "Used Out")) {
$gsm_inuse_4 = 1;
}

$gsm_inuse_all = $gsm_inuse_1+$gsm_inuse_2+$gsm_inuse_3+$gsm_inuse_4;

if ($gsm_inuse_all >= $warning) {
$status = 1;
}
if ($gsm_inuse_all >= $critical) {
$status = 2;
}

if ($statushost == 3) {
$status = 3;
}

my $perfdata="| 'gsminuse'=$gsm_inuse_all;$warning;$critical;";

if ($status == 0){
$temp = sprintf "$issue - GSM Channels In Use: $gsm_inuse_all $perfdata";
}
else {
$temp = sprintf "$issue - GSM Channels In Use: $gsm_inuse_all $perfdata";
}
append($temp);
}

####################################################################
# help and usage information #
####################################################################

sub usage {
print << "USAGE";
-----------------------------------------------------------------
$script v$script_version

Monitors Quescom GSM Gateways.

Usage: $script -H -s -t -C -w -c

Options: -H Hostname or IP address
-S Service to check (gsmsignal,gsminuse,voipinusetotal)
-t GSM Trunk to check (1,2,3,4) or Foreign Gatekeeper to check (1,2,3,4)
-C Community (default is public)
-w Warning Value
-c Critical Value
-----------------------------------------------------------------
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
exit 1;
}

####################################################################
# Appends string to existing $returnstring #
####################################################################

sub append {
my $appendstring = @_[0];
$returnstring = "$returnstring$appendstring";
}
PS: O script por enquanto suporta apenas a checagem de 4 Chips GSM e 4 Foreign Gatekeeper. Dúvidas? Use rode o script sem parametro para ver o help. Ou deixe um comentário.
If you want this in English, leave a comment below!

Melhorando o Hyper-V (Live Migration on Shutdown)

Por default o Hyper-V em Cluster irá fazer Quick Migration das máquinas para outro nó do cluster quando você pedir para desligar a máquina.
Para melhorar esse processo é possivel, através de Script de PowerShell fazer o Live Migration.

Encontrei esse post relatando como fazer: Improve your Hyper-V Virtual Availability - Live Migrate VMs on Shutdown - Deliberations from Dave

Apenas fiz algumas alterações no Script para: 1) Funcionar com um Cluster de apenas 2 nodes e 2) Fazer o Live Migration somente das maquinas que tem como Possible Owner o outro node deixando assim a operação mais rapida.

O script alterado ficou com a seguinte cara:


ImportSystemModules

$Computer = Get-Content env:ComputerName

Suspend-ClusterNode $Computer

$AvailNodes = (get-clusternode | Where-Object { $_.State -eq "Up" -and $_.Name -ne "$Computer" })
$i = 0

$VMGroups = Get-ClusterNode $Computer| Get-ClusterGroup | ?{ $_ | Get-ClusterResource | ?{ $_.ResourceType -like "Virtual Machine" } | Get-ClusterOwnerNode | Where-Object { $_.OwnerNodes -like $AvailNodes } }
foreach ( $VMGroup in $VMGroups ) {
Move-ClusterVirtualMachineRole "$VMGroup" -Node $AvailNodes.Name
$i = $i + 1
if ($i -eq $AvailNodes.Count) { $i = 0 }
}

Resume-ClusterNode $Computer


Com isso é só seguir a receita de bolo do link acima e está funcionando.

Update de versão do NagVis

Para fazer o update de versão de um NagVis já instalado basta seguir os passos abaixo:

# cd /usr/local/src/
# wget http://sourceforge.net/projects/nagvis/files/NagVis%201.5/nagvis-1.5.7.tar.gz/download
# tar -xzf nagvis-1.5.7.tar.gz
# cd nagvis-1.5.7
# ./install.sh

Seguir as instruções do script de instalação escolhendo o Backend correto! (Cuidado pois o script nao identifica o backend utilizado no sistema que está no ar).
Se houver algum Gadget modificado manualmente lembrar de copiar do "backup directory" (o script irá mostrar o caminho) /share/userfiles/gadgets para /usr/local/nagvis/share/userfiles/gadgets.
Isso é tudo. Já deve esta funcionando!

Site no ar?

Como checar se seu site esta disponível para a internet (sem nenhum conhecido conectado para fazer isso por você :o) )?
Is it up? Resolve seu problema!


Monitorando Nobreaks APC com Nagios

Encontrei um excelente script que retorna muitas informações sobre o nobreak, incluindo perfdata com várias informações, porém não inclui o tempo de bateria restante no perfdata e também não permite configurar thresholds para warning e critical. Originalmente encontrado em http://www.neteye-blog.it/?p=524


#!/usr/bin/perl

# Copyright (C) 2004 Altinity Limited
# E: info@altinity.com W: http://www.altinity.com/
#
# Edited by Roderick Derks (roderick@r71.nl)
# I changed the output of this plugin so it can bu used with Nagiosgrapher
# to create nice graphs (output is in hours is changed to minutes).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Adjusted by Roderick Derks (roderick@r71.nl)
# Output of snmp query for remaing battery runtime is in minutes or in hours. This
# is a problem when you want to create some nice graphs because of the different
# output. Now hours are converted to minutes.
#

use Net::SNMP;
use Getopt::Std;

$script = "check_snmp_apcups";
$script_version = "2.1.0";

$metric = 1;

$ipaddress = "192.168.1.1"; # default IP address, if none supplied
$version = "1"; # SNMP version
$timeout = 2; # SNMP query timeout
# $warning = 100;
# $critical = 150;
$status = 0;
$returnstring = "";

$community = "public"; # Default community string


# .1.3.6.1.4.1.
# enterprises.318.1.1.1.1.1.1.0 = STRING: "SMART-UPS 1000" upsIdent

# enterprises.318.1.1.1.2.2.1.0 Battery capacity (%)
# enterprises.318.1.1.1.2.2.2.0 Temperature (Celcius)
# enterprises.318.1.1.1.2.2.3.0 Battery runtime remaining
# enterprises.318.1.1.1.2.2.4.0 Battery replace indicator (1=ok, 2=replace)
# enterprises.318.1.1.1.2.2.5.0 Number of battery packs
# enterprises.318.1.1.1.2.2.6.0 Number of bad battery packs

# enterprises.318.1.1.1.3.2.1.0 Input voltage
# enterprises.318.1.1.1.3.2.4.0 Input frequency
# enterprises.318.1.1.1.3.2.5.0 Reason for last transfer to UPS battery power:

# enterprises.318.1.1.1.4.2.1.0 Output voltage
# enterprises.318.1.1.1.4.2.2.0 Output frequency
# enterprises.318.1.1.1.4.2.3.0 Output load as % of capacity
# enterprises.318.1.1.1.4.2.4.0 Output current in ampheres

# enterprises.318.1.1.1.4.2.1.0 Configured voltage
# enterprises.318.1.1.1.8.1.0 Whether agent is communicating with UPS (1)

$oid_upstype = ".1.3.6.1.4.1.318.1.1.1.1.1.1.0";
$oid_battery_capacity = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
$oid_battery_temperature = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
$oid_battery_runtimeremain = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
$oid_battery_replace = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
$oid_input_voltage = ".1.3.6.1.4.1.318.1.1.1.3.2.1.0";
$oid_input_frequency = ".1.3.6.1.4.1.318.1.1.1.3.2.4.0";
$oid_input_reasonforlasttransfer = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
$oid_output_voltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_output_frequency = ".1.3.6.1.4.1.318.1.1.1.4.2.2.0";
$oid_output_load = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
$oid_output_current = ".1.3.6.1.4.1.318.1.1.1.4.2.4.0";
$oid_output_configuredvoltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_comms = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
$oid_test_result = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";
$oid_test_date = ".1.3.6.1.4.1.318.1.1.1.7.2.4.0";
$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";

$upstype = "";
$battery_capacity = 0;
$battery_temperature = 0;
$battery_runtimeremain = 0;
$battery_replace = "";
$input_voltage = 0;
$input_frequency = 0;
$input_reasonforlasttransfer = "";
$output_voltage = 0;
$output_frequency = 0;
$output_load = 0;
$output_current = 0;
$output_configuredvoltage = 0;
$outagecause = "";
$test_result = "";
$test_date = "";


# Do we have enough information?
if (@ARGV < 1) {
print "Too few arguments\n";
usage();
}

getopts("h:H:C:w:c:");
if ($opt_h){
usage();
exit(0);
}
if ($opt_H){
$hostname = $opt_H;
}
else {
print "No hostname specified\n";
usage();
}
if ($opt_C){
$community = $opt_C;
}
else {
}



# Create the SNMP session
my ($s, $e) = Net::SNMP->session(
-community => $community,
-hostname => $hostname,
-version => $version,
-timeout => $timeout,
);

main();

# Close the session
$s->close();

if ($returnstring eq ""){
$status = 3;
}

if ($status == 0){
print "Status is OK - $returnstring\n";
# print "$returnstring\n";
}
elsif ($status == 1){
print "Status is a WARNING level - $returnstring\n";
}
elsif ($status == 2){
print "Status is CRITICAL - $returnstring\n";
}
else{
print "Problem with plugin. No response from SNMP agent.\n";
}

exit $status;


####################################################################
# This is where we gather data via SNMP and return results #
####################################################################

sub main {

#######################################################

if (!defined($s->get_request($oid_comms))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$temp = $s->var_bind_list()->{$_};
}

if ($temp eq "1"){
}
else {
append("SNMP agent not communicating with UPS");
$status = 2;
return 1;
}


#######################################################

if (!defined($s->get_request($oid_upstype))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$upstype = $s->var_bind_list()->{$_};
}

#######################################################

if (!defined($s->get_request($oid_battery_capacity))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_capacity = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_battery_temperature))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_temperature = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_battery_runtimeremain))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_runtimeremain = $s->var_bind_list()->{$_};
}

# RRD if output is in hours, change it to minutes so we can make a nice graph
if ( $battery_runtimeremain =~ "hour" ) {
$battery_runtimeremain2 = "$battery_runtimeremain";
$battery_runtimeremain =~s/hour.*//g;
$battery_runtimeremain = $battery_runtimeremain*60;
$battery_runtimeremain = "$battery_runtimeremain minutes";
}
$battery_runtime = $battery_runtimeremain;
$battery_runtime =~s/minutes.*//g;

#######################################################

if (!defined($s->get_request($oid_battery_replace))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_replace = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_input_voltage))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$input_voltage = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_input_frequency))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$input_frequency = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_input_reasonforlasttransfer))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$input_reasonforlasttransfer = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_output_voltage))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$output_voltage = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_output_frequency))) {
if (!defined($s->get_request($oid_sysDescr))) { $returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$output_frequency = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_output_load))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$output_load = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_test_result))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$test_result = $s->var_bind_list()->{$_};
}
#######################################################

if (!defined($s->get_request($oid_test_date))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$test_date = $s->var_bind_list()->{$_};
}
#######################################################

$issue = "";

if ($input_reasonforlasttransfer eq "1"){
$outagecause = "No events"
}
elsif ($input_reasonforlasttransfer eq "2"){
$outagecause = "High line voltage"
}
elsif ($input_reasonforlasttransfer eq "3"){
$outagecause = "Brownout"
}
elsif ($input_reasonforlasttransfer eq "4"){
$outagecause = "Loss of mains power"
}
elsif ($input_reasonforlasttransfer eq "5"){
$outagecause = "Small temporary power drop"
}
elsif ($input_reasonforlasttransfer eq "6"){
$outagecause = "Large temporary power drop"
}
elsif ($input_reasonforlasttransfer eq "7"){
$outagecause = "Small spike"
}
elsif ($input_reasonforlasttransfer eq "8"){
$outagecause = "Large spike"
}
elsif ($input_reasonforlasttransfer eq "9"){
$outagecause = "UPS self test"
}
elsif ($input_reasonforlasttransfer eq "10"){
$outagecause = "Excessive input voltage fluctuation"
}
else {
$outagecause = "Cannot establish reason"
}


if ($test_result eq "1") {
$test_result_string = "Passed";
}
elsif ($test_result eq "2") {
$test_result_string = "Failed";
}
elsif ($test_result eq "4") {
$test_result_string = "In Progress";
}
else {
$test_result_string = "Unknown";
}


if ($battery_capacity < 50) {
$issue = $issue . "BATTERY CAPACITY WARNING! ";
$status = 1;
}
if ($output_load > 80) {
$status = 1;
$issue = $issue . "OUTPUT LOAD WARNING! ";
}
if ($test_result eq "2") {
$issue = $issue . "SELF TEST FAILED! ";
$status = 1;
}
if ($input_voltage < 1){
$status = 2;
$issue = $issue . "RUNNING ON BATTERY! ";
}
if ($battery_capacity < 25) {
$issue = $issue . "BATTERY RUNNING LOW! ";
$status = 2;
}
if ($output_load > 90) {
$issue = $issue . "HIGH OUTPUT LOAD! ";
$status = 2;
}
if ($battery_replace eq "2") {
$issue = $issue . "REPLACE BATTERY! ";
$status = 2;
}



if ($status == 0){
$temp = sprintf "$upstype - BATTERY:(capacity $battery_capacity%%, temperature $battery_temperature C, runtime $battery_runtimeremain) INPUT:(voltage $input_voltage V, frequency $input_frequency Hz) OUTPUT:(voltage $output_voltage V, frequency $output_frequency Hz, load $output_load%%) SELF TEST:($test_result_string on $test_date) LAST EVENT:($outagecause)";
}
else {
$temp = sprintf "$issue - $upstype - BATTERY:(capacity $battery_capacity%%, temperature $battery_temperature C, runtime $battery_runtimeremain) INPUT:(voltage $input_voltage V, frequency $input_frequency Hz) OUTPUT:(voltage $output_voltage V, frequency $output_frequency Hz, load $output_load%%) LAST EVENT:$outagecause";
}
$perfdata = sprintf "capacity=$battery_capacity%%;50;25;0;100 load=$output_load%%;80;90;0;100 temp=$battery_temperature runtime=$battery_runtime iv=$input_voltage ov=$output_voltage";
#$perfdata = sprintf "capacity=$battery_capacity load=$output_load temp=$battery_temperature runtime=$battery_runtime in_vol=$input_voltage in_freq=$input_frequency out_vol=$output_voltage out_freq=$output_frequency";
append($temp);
append("|");
append($perfdata);
}

####################################################################
# help and usage information #
####################################################################

sub usage {
print << "USAGE";
-----------------------------------------------------------------
$script v$script_version

Monitors APC SmartUPS via AP9617 SNMP management card.

Usage: $script -H -c [...]

Options: -H Hostname or IP address
-C Community (default is public)

-----------------------------------------------------------------
Copyright 2004 Altinity Limited

This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
exit 1;
}

####################################################################
# Appends string to existing $returnstring #
####################################################################

sub append {
my $appendstring = @_[0];
$returnstring = "$returnstring$appendstring";
}

Com algumas modificações desse script, escrevi outros 2 conforme abaixo:


1) check_snmp_apc_ups_runtime
Esse script permite passar os parametros de warning (-w) e critical (-c) e irá trazer no perfdata somente a informação de quantos minutos de bateria restante o nobreak possui (battery_runtime). O script também considera que se o nobreak estiver alimentado (output_status = Online) o resultado sempre será OK. Somente se o nobreak estiver com um Output Status diferente de Online (incluindo On Battery) que os parametros de warning e critical serão levados em consideração.


#!/usr/bin/perl

# Copyright (C) 2004 Altinity Limited
# E: info@altinity.com W: http://www.altinity.com/
#
# Edited by Roderick Derks (roderick@r71.nl)
# I changed the output of this plugin so it can bu used with Nagiosgrapher
# to create nice graphs (output is in hours is changed to minutes).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Adjusted by Roderick Derks (roderick@r71.nl)
# Output of snmp query for remaing battery runtime is in minutes or in hours. This
# is a problem when you want to create some nice graphs because of the different
# output. Now hours are converted to minutes.
#

use Net::SNMP;
use Getopt::Std;

$script = "check_snmp_apc_ups_runtime";
$script_version = "2.1.0";

$metric = 1;

$ipaddress = "192.168.1.1"; # default IP address, if none supplied
$version = "1"; # SNMP version
$timeout = 2; # SNMP query timeout
$warning = 52;
$critical = 46;
$status = 0;
$returnstring = "";

$community = "public"; # Default community string

# .1.3.6.1.4.1.
# enterprises.318.1.1.1.1.1.1.0 = STRING: "SMART-UPS 1000" upsIdent

# enterprises.318.1.1.1.2.2.1.0 Battery capacity (%)
# enterprises.318.1.1.1.2.2.2.0 Temperature (Celcius)
# enterprises.318.1.1.1.2.2.3.0 Battery runtime remaining
# enterprises.318.1.1.1.2.2.4.0 Battery replace indicator (1=ok, 2=replace)
# enterprises.318.1.1.1.2.2.5.0 Number of battery packs
# enterprises.318.1.1.1.2.2.6.0 Number of bad battery packs

# enterprises.318.1.1.1.3.2.1.0 Input voltage
# enterprises.318.1.1.1.3.2.4.0 Input frequency
# enterprises.318.1.1.1.3.2.5.0 Reason for last transfer to UPS battery power:

# enterprises.318.1.1.1.4.2.1.0 Output voltage
# enterprises.318.1.1.1.4.2.2.0 Output frequency
# enterprises.318.1.1.1.4.2.3.0 Output load as % of capacity
# enterprises.318.1.1.1.4.2.4.0 Output current in ampheres

# enterprises.318.1.1.1.4.2.1.0 Configured voltage
# enterprises.318.1.1.1.8.1.0 Whether agent is communicating with UPS (1)

$oid_upstype = ".1.3.6.1.4.1.318.1.1.1.1.1.1.0";
$oid_battery_capacity = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
$oid_battery_temperature = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
$oid_battery_runtimeremain = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
#$oid_battery_runtimeremain = ".1.3.6.1.2.1.2.2.1.9.20";
$oid_battery_replace = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
$oid_input_voltage = ".1.3.6.1.4.1.318.1.1.1.3.2.1.0";
$oid_input_frequency = ".1.3.6.1.4.1.318.1.1.1.3.2.4.0";
$oid_input_reasonforlasttransfer = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
$oid_output_voltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_output_frequency = ".1.3.6.1.4.1.318.1.1.1.4.2.2.0";
$oid_output_load = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
$oid_output_current = ".1.3.6.1.4.1.318.1.1.1.4.2.4.0";
$oid_output_configuredvoltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_comms = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
$oid_test_result = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";
$oid_test_date = ".1.3.6.1.4.1.318.1.1.1.7.2.4.0";
$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
$oid_output_status = ".1.3.6.1.4.1.318.1.1.1.4.1.1.0";

$upstype = "";
$battery_capacity = 0;
$battery_temperature = 0;
$battery_runtimeremain = 0;
$battery_replace = "";
$input_voltage = 0;
$input_frequency = 0;
$input_reasonforlasttransfer = "";
$output_voltage = 0;
$output_frequency = 0;
$output_load = 0;
$output_current = 0;
$output_status = 0;
$output_configuredvoltage = 0;
$outagecause = "";
$test_result = "";
$test_date = "";


# Do we have enough information?
if (@ARGV < 1) {
print "Too few arguments\n";
usage();
}

getopts("h:H:C:w:c:");
if ($opt_h){
usage();
exit(0);
}
if ($opt_H){
$hostname = $opt_H;
}
else {
print "No hostname specified\n";
usage();
}
if ($opt_C){
$community = $opt_C;
}
if ($opt_c){
$critical = $opt_c;
}
else {
print "No critical value specified\n";
usage();
}
if ($opt_w){
$warning = $opt_w;
}
else {
print "No warning value specified\n";
usage();
}

# Create the SNMP session
my ($s, $e) = Net::SNMP->session(
-community => $community,
-hostname => $hostname,
-version => $version,
-timeout => $timeout,
);

main();

# Close the session
$s->close();

if ($returnstring eq ""){
$status = 3;
}

if ($status == 0){
print "Status is OK $returnstring\n";
# print "$returnstring\n";
}
elsif ($status == 1){
print "Status is a WARNING $returnstring\n";
}
elsif ($status == 2){
print "Status is CRITICAL $returnstring\n";
}
else{
print "Problem with plugin. No response from SNMP agent.\n";
}

exit $status;


####################################################################
# This is where we gather data via SNMP and return results #
####################################################################

sub main {

#######################################################

if (!defined($s->get_request($oid_comms))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}

}
foreach ($s->var_bind_names()) {
$temp = $s->var_bind_list()->{$_};
}

if ($temp eq "1"){
}
else {
append("SNMP agent not communicating with UPS");
$status = 2;
return 1;
}


#######################################################

if (!defined($s->get_request($oid_upstype))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$upstype = $s->var_bind_list()->{$_};
}

#######################################################

if (!defined($s->get_request($oid_battery_capacity))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_capacity = $s->var_bind_list()->{$_};
}

#######################################################

if (!defined($s->get_request($oid_output_status))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$output_status = $s->var_bind_list()->{$_};
}

#######################################################

if (!defined($s->get_request($oid_battery_runtimeremain))) {
if (!defined($s->get_request($oid_sysDescr))) {
$returnstring = "SNMP agent not responding";
$status = 1;
return 1;
}
else {
$returnstring = "SNMP OID does not exist";
$status = 1;
return 1;
}
}
foreach ($s->var_bind_names()) {
$battery_runtimeremain = $s->var_bind_list()->{$_};
}

# Normalize RRD so we can make a nice graph
$batteryAdvRuntimehour = $battery_runtimeremain;
$batteryAdvRuntimehour =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/1 hour, 0(\d*):\d*.\d*/1/;
$batteryAdvRuntimehour =~ s/1 hour, (\d*):\d*.\d*/1/;
$batteryAdvRuntimeminute = $battery_runtimeremain;
$batteryAdvRuntimeminute =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/1 hour, 0(\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/1 hour, (\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/0(\d*):(\d*).\d*/$1 mins/;
$batteryAdvRuntimeminute =~ s/(\d*):(\d*).\d*/$1 mins/;

$battery_runtimeremain = eval($batteryAdvRuntimehour * 60 + $batteryAdvRuntimeminute),"\n";
$battery_runtimeremain_display = "$battery_runtimeremain Minutos";
$battery_runtimeremain_perfdata = "$battery_runtimeremain Minutos";
$battery_runtimeremain_perfdata =~s/ //g;
#######################################################

$issue = "";

if ($output_status eq "1"){
$output_status_display = "Unknown"
}
elsif ($output_status eq "2"){
$output_status_display = "On Line"
}
elsif ($output_status eq "3"){
$output_status_display = "On Battery"
}
elsif ($output_status eq "4"){
$output_status_display = "On Smart Boost"
}
elsif ($output_status eq "5"){
$output_status_display = "Timed Sleeping"
}
elsif ($output_status eq "6"){
$output_status_display = "Software Bypass"
}
elsif ($output_status eq "7"){
$output_status_display = "Off"
}
elsif ($output_status eq "8"){
$output_status_display = "Rebooting"
}
elsif ($output_status eq "9"){
$output_status_display = "Switched Bypass"
}
elsif ($output_status eq "10"){
$output_status_display = "Hardware Failure Bypass"
}
elsif ($output_status eq "11"){
$output_status_display = "Sleeping Until Power Returns"
}
elsif ($output_status eq "12"){
$output_status_display = "On Smart Trim"
}
else {
$output_status_display = "Cannot establish status"
}


if ($output_status eq "2"){
$status = 0;
}
else {
if ($battery_runtimeremain <= $warning) {
# $issue = $issue . "TEMPO DE BATERIA - WARNING! ";
$status = 1;
}

if ($battery_runtimeremain <= $critical) {
# $issue = $issue . "TEMPO DE BATERIA - CRITICAL! ";
$status = 2;
}
}



# Modified by Joao Vitor Arruda to perfdata runtime in minutes
# Modified 2010-11-24
my $perfdata="| 'battery_runtime'=$battery_runtimeremain_perfdata;$warning;$critical";

if ($status == 0){
$temp = sprintf "$issue - $upstype - Output Status: $output_status_display - Battery Info:(capacity $battery_capacity%%, runtime $battery_runtimeremain_display) $perfdata";
}
else {
$temp = sprintf "$issue - $upstype - Output Status: $output_status_display - Battery Info:(capacity $battery_capacity%%, runtime $battery_runtimeremain_display) $perfdata ";
}
append($temp);
}

####################################################################
# help and usage information #
####################################################################

sub usage {
print << "USAGE";
-----------------------------------------------------------------
$script v$script_version

Monitors APC SmartUPS via AP9617 SNMP management card.

Usage: $script -H -C -w -c

Options: -H Hostname or IP address
-C Community (default is public)
-w Warning Value
-c Critical Value
-----------------------------------------------------------------
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
exit 1;
}

####################################################################
# Appends string to existing $returnstring #
####################################################################

sub append {
my $appendstring = @_[0];
$returnstring = "$returnstring$appendstring";
}

2) check_snmp_apc_ups_datacenter (um pouco sem ideia para o nome)
Esse script contém as mesmas alterações do acima, porém agora ele checa 2 Nobreaks de uma só vez (utilizando os parametos -H para o primeiro e -F para o segundo). Neste caso somente se ambos os nobreaks estiverem com Output Status diferente de Online que os parametros de warning e critical serão levados em consideração. No perfdata será apresentado o os minutos de bateria restante em cada nobreak bem como a média de ambos (battery_runtime1, battery_runtime2 e combined_runtime). A ideia desse script é monitorar um ambiente onde 2 nobreak são utilizados para ligar equipamentos com fontes redundantes.


#!/usr/bin/perl
# Verifica Status e Tempo de Bateria de 02 Nobreaks (Host1 e Host2)

use Net::SNMP;
use Getopt::Std;

$script = "check_snmp_apc_ups_datacenter";
$script_version = "2.1.0";

$metric = 1;

$ipaddress = "192.168.1.1"; # default IP address, if none supplied
$version = "1"; # SNMP version
$timeout = 2; # SNMP query timeout
$warning = 52;
$critical = 46;
$status = 0;
$returnstring = "";
$community = "public"; # Default community string

$oid_battery_capacity = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
$oid_battery_runtimeremain = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
$oid_comms = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
$oid_output_status = ".1.3.6.1.4.1.318.1.1.1.4.1.1.0";

$battery_capacity_h1 = 0;
$battery_runtimeremain_h1 = 0;
$battery_runtimeremain_perfdata_h1 = 0;
$output_status_h1 = 0;
$battery_capacity_h2 = 0;
$battery_runtimeremain_h2 = 0;
$battery_runtimeremain_perfdata_h2 = 0;
$output_status_h2 = 0;
$battery_capacity_c = 0;
$battery_runtimeremain_display_c = 0;

# Do we have enough information?
if (@ARGV < 1) {
print "Too few arguments\n";
usage();
}

getopts("h:H:F:C:w:c:");
if ($opt_h){
usage();
exit(0);
}
if ($opt_H){
$hostname1 = $opt_H;
}
else {
print "No hostname 1 (H) specified\n";
usage();
}
if ($opt_F){
$hostname2 = $opt_F;
}
else {
print "No hostname 2 (F) specified\n";
usage();
}
if ($opt_C){
$community = $opt_C;
}
if ($opt_c){
$critical = $opt_c;
}
else {
print "No critical value specified\n";
usage();
}
if ($opt_w){
$warning = $opt_w;
}
else {
print "No warning value specified\n";
usage();
}

# Create the SNMP session for Host 1
my ($s1, $e1) = Net::SNMP->session(
-community => $community,
-hostname => $hostname1,
-version => $version,
-timeout => $timeout,
);

main1();
# Close the session for Host 1
$s1->close();

# Create the SNMP session for Host 2
my ($s2, $e2) = Net::SNMP->session(
-community => $community,
-hostname => $hostname2,
-version => $version,
-timeout => $timeout,
);

main2();
# Close the session for Host 2
$s2->close();

verify();

if ($returnstring eq ""){
$status = 3;
}

if ($status == 0){
print "Status is OK $returnstring\n";
# print "$returnstring\n";
}
elsif ($status == 1){
print "Status is a WARNING $returnstring\n";
}
elsif ($status == 2){
print "Status is CRITICAL $returnstring\n";
}
elsif ($status == 3){
print "Status is UNKNOWN $returnstring\n";
}
else{
print "Problem with plugin. No response from SNMP agent.\n";
}

exit $status;


####################################################################
# This is where we gather data via SNMP and return results #
####################################################################

sub main1 {

#######################################################
if (!defined($s1->get_request($oid_comms))) {
}
foreach ($s1->var_bind_names()) {
$temp = $s1->var_bind_list()->{$_};
}
if ($temp eq "1"){
}
else {
append("- SNMP agent not communicating with UPS 1");
$statushost1 = 3;
return 1;
}

#######################################################
if (!defined($s1->get_request($oid_battery_capacity))) {
}
foreach ($s1->var_bind_names()) {
$battery_capacity_h1 = $s1->var_bind_list()->{$_};
}

#######################################################
if (!defined($s1->get_request($oid_output_status))) {
}
foreach ($s1->var_bind_names()) {
$output_status_h1 = $s1->var_bind_list()->{$_};
}

#######################################################
if (!defined($s1->get_request($oid_battery_runtimeremain))) {
}
foreach ($s1->var_bind_names()) {
$battery_runtimeremain_h1 = $s1->var_bind_list()->{$_};
}
# Normalize RRD so we can make a nice graph
$batteryAdvRuntimehour = $battery_runtimeremain_h1;
$batteryAdvRuntimehour =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/1 hour, 0(\d*):\d*.\d*/1/;
$batteryAdvRuntimehour =~ s/1 hour, (\d*):\d*.\d*/1/;
$batteryAdvRuntimeminute = $battery_runtimeremain_h1;
$batteryAdvRuntimeminute =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/1 hour, 0(\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/1 hour, (\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/0(\d*):(\d*).\d*/$1 mins/;
$batteryAdvRuntimeminute =~ s/(\d*):(\d*).\d*/$1 mins/;

$battery_runtimeremain_h1 = eval($batteryAdvRuntimehour * 60 + $batteryAdvRuntimeminute),"\n";
$battery_runtimeremain_perfdata_h1 = "$battery_runtimeremain_h1 Minutos";
$battery_runtimeremain_perfdata_h1 =~s/ //g;
#######################################################
}

sub main2 {

#######################################################
if (!defined($s2->get_request($oid_comms))) {
}
foreach ($s2->var_bind_names()) {
$temp = $s2->var_bind_list()->{$_};
}
if ($temp eq "1"){
}
else {
append("- SNMP agent not communicating with UPS 2");
$statushost2 = 3;
return 1;
}

#######################################################
if (!defined($s2->get_request($oid_battery_capacity))) {
}
foreach ($s2->var_bind_names()) {
$battery_capacity_h2 = $s2->var_bind_list()->{$_};
}

#######################################################
if (!defined($s2->get_request($oid_output_status))) {
}
foreach ($s2->var_bind_names()) {
$output_status_h2 = $s2->var_bind_list()->{$_};
}

#######################################################
if (!defined($s2->get_request($oid_battery_runtimeremain))) {
}
foreach ($s2->var_bind_names()) {
$battery_runtimeremain_h2 = $s2->var_bind_list()->{$_};
}
# Normalize RRD so we can make a nice graph
$batteryAdvRuntimehour = $battery_runtimeremain_h2;
$batteryAdvRuntimehour =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$1/;
$batteryAdvRuntimehour =~ s/1 hour, 0(\d*):\d*.\d*/1/;
$batteryAdvRuntimehour =~ s/1 hour, (\d*):\d*.\d*/1/;
$batteryAdvRuntimeminute = $battery_runtimeremain_h2;
$batteryAdvRuntimeminute =~ s/(\d*) hours, 0(\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/(\d*) hours, (\d*):(\d*).(\d*)/$2/;
$batteryAdvRuntimeminute =~ s/1 hour, 0(\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/1 hour, (\d*):(\d*).\d*/$1/;
$batteryAdvRuntimeminute =~ s/0(\d*):(\d*).\d*/$1 mins/;
$batteryAdvRuntimeminute =~ s/(\d*):(\d*).\d*/$1 mins/;

$battery_runtimeremain_h2 = eval($batteryAdvRuntimehour * 60 + $batteryAdvRuntimeminute),"\n";
$battery_runtimeremain_perfdata_h2 = "$battery_runtimeremain_h2 Minutos";
$battery_runtimeremain_perfdata_h2 =~s/ //g;
#######################################################
}

sub verify {
$issue = "";

if ($output_status_h1 eq "1"){
$output_status_display_h1 = "Unknown"
}
elsif ($output_status_h1 eq "2"){
$output_status_display_h1 = "On Line"
}
elsif ($output_status_h1 eq "3"){
$output_status_display_h1 = "On Battery"
}
elsif ($output_status_h1 eq "4"){
$output_status_display_h1 = "On Smart Boost"
}
elsif ($output_status_h1 eq "5"){
$output_status_display_h1 = "Timed Sleeping"
}
elsif ($output_status_h1 eq "6"){
$output_status_display_h1 = "Software Bypass"
}
elsif ($output_status_h1 eq "7"){
$output_status_display_h1 = "Off"
}
elsif ($output_status_h1 eq "8"){
$output_status_display_h1 = "Rebooting"
}
elsif ($output_status_h1 eq "9"){
$output_status_display_h1 = "Switched Bypass"
}
elsif ($output_status_h1 eq "10"){
$output_status_display_h1 = "Hardware Failure Bypass"
}
elsif ($output_status_h1 eq "11"){
$output_status_display_h1 = "Sleeping Until Power Returns"
}
elsif ($output_status_h1 eq "12"){
$output_status_display_h1 = "On Smart Trim"
}
else {
$output_status_display_h1 = "Cannot establish status"
}

if ($output_status_h2 eq "1"){
$output_status_display_h2 = "Unknown"
}
elsif ($output_status_h2 eq "2"){
$output_status_display_h2 = "On Line"
}
elsif ($output_status_h2 eq "3"){
$output_status_display_h2 = "On Battery"
}
elsif ($output_status_h2 eq "4"){
$output_status_display_h2 = "On Smart Boost"
}
elsif ($output_status_h2 eq "5"){
$output_status_display_h2 = "Timed Sleeping"
}
elsif ($output_status_h2 eq "6"){
$output_status_display_h2 = "Software Bypass"
}
elsif ($output_status_h2 eq "7"){
$output_status_display_h2 = "Off"
}
elsif ($output_status_h2 eq "8"){
$output_status_display_h2 = "Rebooting"
}
elsif ($output_status_h2 eq "9"){
$output_status_display_h2 = "Switched Bypass"
}
elsif ($output_status_h2 eq "10"){
$output_status_display_h2 = "Hardware Failure Bypass"
}
elsif ($output_status_h2 eq "11"){
$output_status_display_h2 = "Sleeping Until Power Returns"
}
elsif ($output_status_h2 eq "12"){
$output_status_display_h2 = "On Smart Trim"
}
else {
$output_status_display_h2 = "Cannot establish status"
}

$battery_capacity_c = ($battery_capacity_h1 + $battery_capacity_h2)/2;
$battery_runtimeremain_c = ($battery_runtimeremain_h1 + $battery_runtimeremain_h2)/2;
$battery_runtimeremain_perfdata_c = "$battery_runtimeremain_c Minutos";
$battery_runtimeremain_perfdata_c =~s/ //g;

if ($output_status_h1 eq "2"){
$status = 0;
}
elsif ($output_status_h2 eq "2"){
$status = 0;
}
else {
if ($battery_runtimeremain_c <= $warning) {
$status = 1;
}
if ($battery_runtimeremain_c <= $critical) {
$status = 2;
}
}
if (($statushost1 == 3) && ($statushost2 == 3)) {
$status = 3;
}
# Modified by Joao Vitor Arruda to perfdata runtime in minutes
# Modified 2010-11-24
my $perfdata="| 'battery_runtime1'=$battery_runtimeremain_perfdata_h1;$warning;$critical; 'battery_runtime2'=$battery_runtimeremain_perfdata_h2;$warning;$critical; 'combined_runtime'=$battery_runtimeremain_perfdata_c;$warning;$critical;";

if ($status == 0){
$temp = sprintf "$issue - Status UPS: ($output_status_display_h1)/($output_status_display_h2) - Battery Combined Info:(capacity $battery_capacity_c%%, runtime $battery_runtimeremain_c Minutos) $perfdata";
}
else {
$temp = sprintf "$issue - Status UPS: ($output_status_display_h1)/($output_status_display_h2) - Battery Combined Info:(capacity $battery_capacity_c%%, runtime $battery_runtimeremain_c Minutos) $perfdata ";
}
append($temp);
}

####################################################################
# help and usage information #
####################################################################

sub usage {
print << "USAGE";
-----------------------------------------------------------------
$script v$script_version

Monitors APC SmartUPS via AP9617 SNMP management card.

Usage: $script -H -F -C -w -c

Options: -H Hostname 1 or IP address 1
-F Hostname 2 or IP address 2
-C Community (default is public)
-w Warning Value
-c Critical Value
-----------------------------------------------------------------
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
exit 1;
}

####################################################################
# Appends string to existing $returnstring #
####################################################################

sub append {
my $appendstring = @_[0];
$returnstring = "$returnstring$appendstring";
}