设为首页 收藏本站
查看: 920|回复: 0

nagios磁盘利用率监控

[复制链接]
发表于 2019-1-15 12:17:25 | 显示全部楼层 |阅读模式
  #!/usr/bin/env perl
###################################
#Version 1.1
#Auth Badboy
#FileName Check_disk_utilization.pl
#LastModify 20120722
###################################
use strict;
use Net::SNMP;
use Nagios::Plugin;

(my $snmpVersion = Net::SNMP->VERSION) =~ s/\D//g;
my $TIMEOUT=15;

my $np=Nagios::Plugin->new(
       shortname=>'CheckDiskUtilization',
       usage=>'Usage: %s -H  -C  -c  -w ',
       version =>'Version 1.1'
);

sub isnotnum { # Return true if arg is not a number
  my $num = shift;
  if ( $num =~ /^-?(\d+\.?\d*)|(^\.\d+)$/ ) { return 0; }
  return 1;
}

$SIG{'ALRM'} = sub {
   $np->nagios_exit(UNKNOWN, "ERROR: Alarm signal (Nagios time-out)");
};

sub verbose {
   my $t = shift;
   print $t, "\n" if $np->opts->verbose > 0;
}

sub check_options{
   $np->add_arg(spec => 'hostname|H=s',   help => 'Hostname or IP address to poll', required => 1, label => [ 'HOSTNAME' ]);
   $np->add_arg(spec => 'community|C=s',  help => 'SNMP community to use when connecting', required => 1, default => 'public', label
=> [ 'COMMUNITY' ]);
  $np->add_arg(spec => 'critical|c=i',   help => 'Return critical status if larger than critical threshold', label => [ 'threshold n
um' ]);
   $np->add_arg(spec => 'warning|w=i',    help => 'Return warning status if larger than warning threshold', label => [ 'threshold nu
m' ]);
   $np->add_arg(spec => 'port|p=i',       help => 'Changes the SNMP port, defaults to 161', default => 161, label => [ 'PORT' ]);
   $np->add_arg(spec => 'snmpv1|1',       help => 'Use SNMP v1 instead of the default version 2c');
   $np->add_arg(spec => 'perfdata',       help => 'Write out performance data (number of disk used size)');

   $np->getopts();

   if (defined($np->opts->timeout) && (isnotnum($np->opts->timeout) || ($np->opts->timeout < 2) || ($np->opts->timeout > 60))) {
      $np->nagios_exit(UNKNOWN, 'Timeout must be between 1 and 60');
   }
   if (!defined($np->opts->critical) || !defined($np->opts->warning) || isnotnum($np->opts->critical) || isnotnum($np->opts->warning
)) {
         $np->nagios_exit(UNKNOWN, 'The values for critical and warning threshold must be integers');
      }
}

check_options();

my $disk_warning=$np->opts->warning;
my $disk_critical=$np->opts->critical;

if (defined($TIMEOUT)) {
   verbose(&quot;Alarm at $TIMEOUT&quot;);
   alarm($TIMEOUT);
} else {
   verbose(&quot;No timeout defined : &quot; . $np->opts->timeout . &quot; + 10&quot;);
   alarm ($np->opts->timeout + 10);
}

#snmp data
my $disk_index_table = &quot;.1.3.6.1.2.1.25.2.3.1.1&quot;;
my $disk_index_result=get_snmp_data($disk_index_table);
my $disk_desrc_table = &quot;.1.3.6.1.2.1.25.2.3.1.3&quot;;
my $disk_desrc_result=get_snmp_data($disk_desrc_table);
my $disk_units_table = &quot;.1.3.6.1.2.1.25.2.3.1.4&quot;;
my $disk_units_result=get_snmp_data($disk_units_table);
my $disk_size_table = &quot;.1.3.6.1.2.1.25.2.3.1.5&quot;;
my $disk_size_result=get_snmp_data($disk_size_table);
my $disk_used_table = &quot;.1.3.6.1.2.1.25.2.3.1.6&quot;;
my $disk_used_result=get_snmp_data($disk_used_table);

my $status=undef;
my $critical_count=0;

foreach my $oid (keys(%$disk_index_result)) {
    my $disk_desrc=${$disk_desrc_result}{$disk_desrc_table.&quot;.&quot;.${$disk_index_result}{$oid}};
    my $disk_total_size=${$disk_size_result}{$disk_size_table.&quot;.&quot;.${$disk_index_result}{$oid}};
    my $disk_used_size=${$disk_used_result}{$disk_used_table.&quot;.&quot;.${$disk_index_result}{$oid}};
    if($disk_desrc!~/Virtual Memory|R:/  and $disk_desrc !~/Physical Memory|R:/ and $disk_total_size !=0 ){
        my $disk_used_utilization=$disk_used_size/$disk_total_size*100;
        if($disk_used_utilization > $disk_critical){
              printf &quot;%.2s Critical %2.2f%%; &quot;,$disk_desrc,$disk_used_utilization;
              $status=2;
              ++$critical_count;
              next;
        }
        elsif($disk_used_utilization >$disk_warning){
             printf &quot;%.2s Warning %2.2f%%; &quot;,$disk_desrc,$disk_used_utilization;
              $status=1;
              next;
        }
        elsif( $status!=1 and $status!=2  ){
                        printf &quot;%.2s OK;&quot;,$disk_desrc;
                        $status=0;
                }
    }            
}
if($critical_count != 0){
    $status=2;
    verbose(&quot;\nPlugin Exit Code=$status&quot;);
}
verbose(&quot;Plugin Exit Code=$status&quot;);
exit $status;

sub get_snmp_data(){
my $oid=shift;
my ($session, $error) = (undef, undef);
if (defined($np->opts->snmpv1)) {
   # SNMPv1 login
   ($session, $error) = Net::SNMP->session(
      -hostname  => $np->opts->hostname,
      -community => $np->opts->community,
      -port      => $np->opts->port,
      -timeout   => $np->opts->timeout
   );
} else {
   # SNMPv2 login
   ($session, $error) = Net::SNMP->session(
      -hostname  => $np->opts->hostname,
      -version   => 2,
      -community => $np->opts->community,
      -port      => $np->opts->port,
      -timeout   => $np->opts->timeout
   );
}

if (!defined($session)) {
   $np->nagios_exit(UNKNOWN, 'SNMP Error: ' . $error);
}

#get snmp data
   my $result = undef;
   $result = ($snmpVersion < 4) ? $session->get_table($oid) : $session->get_table(Baseoid => $oid);

if (!defined($result)) {
   $session->close;
   $np->nagios_exit(UNKNOWN, &quot;ERROR: get snmp data table: &quot; . $session->error);
}
        return $result;
        $session->close();
}
  今天将使用的脚本做了大大调整,供大家参考!
  如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-663581-1-1.html 上篇帖子: 分享一个批量添加nagios的脚本 下篇帖子: Nagios+pnp两键轻松搞定
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表