nagios插件-->内存监控

本文深入探讨了技术背景下的程序员与架构师角色的区别,解析了成为架构师所需的关键技能和思维方式。通过具体实例,揭示了80%的程序员未能晋升为架构师的原因,并提供了提升路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""

    Nagios plugin to report Memory usage by parsing /proc/meminfo
    by L.S. Keijser <keijser@stone-it.com>
    This script takes Cached memory into consideration by adding that
    to the total MemFree value.
    
    modified by jastme ,can used by pnp4nagios

"""

from optparse import OptionParser
import sys

checkmemver = '0.1'

# Parse commandline options:
parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> [ -h ]",version="%prog " + checkmemver)
parser.add_option("-w", "--warning",
    action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
parser.add_option("-c", "--critical",
    action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")
(options, args) = parser.parse_args()


def readLines(filename):
    f = open(filename, "r")
    lines = f.readlines()
    return lines

def readMemValues():
    global memTotal, memCached, memFree
    for line in readLines('/proc/meminfo'):
        if line.split()[0] == 'MemTotal:':
            memTotal = line.split()[1]
        if line.split()[0] == 'MemFree:':
            memFree = line.split()[1]
        if line.split()[0] == 'Cached:':
            memCached = line.split()[1]

def percMem():
    readMemValues()
    return (((int(memFree) + int(memCached)) * 100) / int(memTotal))

def realMem():
    readMemValues()
    return (int(memFree) + int(memCached)) / 1024

def go():
    if not options.crit_threshold:
        print "UNKNOWN: Missing critical threshold value."
        sys.exit(3)
    if not options.warn_threshold:
        print "UNKNOWN: Missing warning threshold value."
        sys.exit(3)
    if int(options.crit_threshold) >= int(options.warn_threshold):
        print "UNKNOWN: Critical percentage can't be equal to or bigger than warning percentage."
        sys.exit(3)
    trueFree = percMem()
    trueMemFree = realMem()
    if int(trueFree) <= int(options.crit_threshold):
        print "CRITICAL: Free memory percentage is less than or equal to %s%% %s %sMB | mem = %sMB;6546;3273;0;32732"  %(str(options.crit_threshold),str(trueFree),str(trueMemFree),str(trueMemFree))
        sys.exit(2)
    if int(trueFree) <= int(options.warn_threshold):
        print "WARNING: Free memory percentage is less than or equal to %s%% %s %sMB | mem = %sMB;6546;3273;0;32732"  %(str(options.warn_threshold),str(trueFree),str(trueMemFree),str(trueMemFree))
        sys.exit(1)
    else:
        print "OK: Free memory percentage is %s%% %sMB | mem = %sMB;6546;3273;0;32732"  %(str(trueFree),str(trueMemFree),str(trueMemFree))
        sys.exit(0)

if __name__ == '__main__':
    go()


转载于:https://my.oschina.net/jastme/blog/278530

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值