<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kbrandt</id>
	<title>OpenVZ Virtuozzo Containers Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kbrandt"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Kbrandt"/>
	<updated>2026-05-15T20:16:28Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Monitoring_/proc/user_beancounters_with_nagios&amp;diff=6607</id>
		<title>Monitoring /proc/user beancounters with nagios</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Monitoring_/proc/user_beancounters_with_nagios&amp;diff=6607"/>
		<updated>2008-10-28T19:10:39Z</updated>

		<summary type="html">&lt;p&gt;Kbrandt: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{DISPLAYTITLE:Monitoring /proc/user_beancounters with nagios}}&lt;br /&gt;
To be added locally on the VZ HN to &amp;lt;code&amp;gt;/etc/nagios/nrpe_local.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Works as nagios-plugin with option '-f' or reports an increase of a failcnt-value by mail if run e.g. as a cronjob with option '-t'. We use it with both cases to be sure that we see a peak in case it happened between the nagios-checks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Copyright (C) 2008 Christian Benke&lt;br /&gt;
# Distributed under the terms of the GNU General Public License v2&lt;br /&gt;
# v0.2 2008-04-04&lt;br /&gt;
# Christian Benke &amp;lt;c.benke  gmail  com&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import string&lt;br /&gt;
import pickle&lt;br /&gt;
import sys&lt;br /&gt;
import getopt&lt;br /&gt;
import re&lt;br /&gt;
&lt;br /&gt;
veid=''&lt;br /&gt;
current_data=dict()&lt;br /&gt;
opts=None&lt;br /&gt;
beancounter_data=None&lt;br /&gt;
picklefilepath='/tmp/beancounters_pickledump'&lt;br /&gt;
&lt;br /&gt;
#-------- find the hostname for each veid ---:&lt;br /&gt;
&lt;br /&gt;
def find_veid(veid):&lt;br /&gt;
        veid_conf=open('/etc/vz/conf/' + str(veid) + '.conf','r')&lt;br /&gt;
        if int(veid) != 0:&lt;br /&gt;
                for line in veid_conf:&lt;br /&gt;
                        if &amp;quot;HOSTNAME&amp;quot; in line:&lt;br /&gt;
                                quotes=re.compile(&amp;quot;\&amp;quot;&amp;quot;)&lt;br /&gt;
                                line=quotes.sub(&amp;quot;&amp;quot;,line)&lt;br /&gt;
                                linefeed=re.compile(&amp;quot;\n&amp;quot;)&lt;br /&gt;
                                line=linefeed.sub(&amp;quot;&amp;quot;,line)&lt;br /&gt;
                                fqdn=re.split('=',line)&lt;br /&gt;
                                hostname=re.split('\.',fqdn[1])[0]&lt;br /&gt;
                                return hostname&lt;br /&gt;
        else:&lt;br /&gt;
                hostname='OpenVZ HN'&lt;br /&gt;
                return hostname&lt;br /&gt;
&lt;br /&gt;
# ---------- send mail in case of a counter-change&lt;br /&gt;
&lt;br /&gt;
def send_mail(count_change):&lt;br /&gt;
        sendmail = &amp;quot;/usr/lib/sendmail&amp;quot; # sendmail location&lt;br /&gt;
        import os&lt;br /&gt;
        p = os.popen(&amp;quot;%s -t&amp;quot; % sendmail, &amp;quot;w&amp;quot;)&lt;br /&gt;
        p.write(&amp;quot;From: root\n&amp;quot;)&lt;br /&gt;
        p.write(&amp;quot;To: to@example.com\n&amp;quot;)&lt;br /&gt;
        p.write(&amp;quot;Subject: Beancounters changed in the last 5 minutes\n&amp;quot;)&lt;br /&gt;
        p.write(&amp;quot;\n&amp;quot;) # blank line separating headers from body&lt;br /&gt;
        p.write(&amp;quot;The Beancounter-failcnt value of the following veid(s) and resource(s) has \n&amp;quot;)&lt;br /&gt;
        p.write(&amp;quot;increased in the last 5 minutes:\n\n&amp;quot;)&lt;br /&gt;
        p.write(count_change)&lt;br /&gt;
        sts = p.close()&lt;br /&gt;
        if sts is not None:&lt;br /&gt;
                print &amp;quot;Sendmail exit status&amp;quot;, sts&lt;br /&gt;
&lt;br /&gt;
#---------- compare the failcnt-values&lt;br /&gt;
&lt;br /&gt;
def cntcheck(data_read,current_data,veid,fields,count,count_change):&lt;br /&gt;
        if data_read and count == True and data_read is not '0': #comparing counters of new data with previous run&lt;br /&gt;
                if data_read[veid][fields[0]][5] &amp;lt; current_data[veid][fields[0]][5]:&lt;br /&gt;
                        hostname=find_veid(veid)&lt;br /&gt;
                        count_change=str(count_change) + str(hostname) + ': ' + str(fields[0]) + ' failcnt has changed from ' + data_read[veid][fields[0]][5] + ' to ' + str(current_data[veid][fields[0]][5]) + '\n'&lt;br /&gt;
        return count_change&lt;br /&gt;
&lt;br /&gt;
#---------- compare the current value with barrier/limit value&lt;br /&gt;
&lt;br /&gt;
def barriercheck(data_read,current_data,veid,fields,count,barrier_break):&lt;br /&gt;
        if count == False:      #comparing current level with barrier/limit&lt;br /&gt;
                if current_data[veid][fields[0]][0] == 'oomguarpages': #for oomguarpages and physpages only the limit-value is relevant&lt;br /&gt;
                        if int(current_data[veid][fields[0]][1]) &amp;gt; int(current_data[veid][fields[0]][4])*0.9:&lt;br /&gt;
                                hostname=find_veid(veid)&lt;br /&gt;
                                barrier_break = str(barrier_break) + str(hostname) + ': ' + str(current_data[veid][fields[0]][0]) + ' '&lt;br /&gt;
                elif current_data[veid][fields[0]][0] == 'physpages':&lt;br /&gt;
                        if int(current_data[veid][fields[0]][1]) &amp;gt; int(current_data[veid][fields[0]][4])*0.9:&lt;br /&gt;
                                hostname=find_veid(veid)&lt;br /&gt;
                                barrier_break = str(barrier_break) + str(hostname) + ': ' + str(current_data[veid][fields[0]][0]) + ' '&lt;br /&gt;
                else:&lt;br /&gt;
                        if int(current_data[veid][fields[0]][1]) &amp;gt; int(current_data[veid][fields[0]][3])*0.9:&lt;br /&gt;
                                hostname=find_veid(veid)&lt;br /&gt;
                                barrier_break = str(barrier_break) + str(hostname) + ': ' + str(current_data[veid][fields[0]][0]) + ' '&lt;br /&gt;
        return barrier_break&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#------------ read user_beancounter and handle the result of the comparison subroutines&lt;br /&gt;
&lt;br /&gt;
def compare_data(beancounter_data,data_read,count):&lt;br /&gt;
        count_change=str()&lt;br /&gt;
        barrier_break=str()&lt;br /&gt;
        for line in beancounter_data:&lt;br /&gt;
                if 'Version' in line or 'uid' in line or 'dummy' in line:&lt;br /&gt;
                        continue&lt;br /&gt;
                else:&lt;br /&gt;
                        fields=line.split( )&lt;br /&gt;
                        if len(fields) == 7:&lt;br /&gt;
                                i=0&lt;br /&gt;
                                veid=int(fields[0][:-1])&lt;br /&gt;
                                fields.pop(0) #remove the first element&lt;br /&gt;
                                current_data[veid]=dict()&lt;br /&gt;
                                current_data[veid][fields[0]]=fields&lt;br /&gt;
                        else:&lt;br /&gt;
                                i=i+1&lt;br /&gt;
                                current_data[veid][fields[0]]=fields&lt;br /&gt;
&lt;br /&gt;
                # ------ check barrier/limit&lt;br /&gt;
                        barrier_break=barriercheck(data_read,current_data,veid,fields,count,barrier_break)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
                # ------ check failcnt&lt;br /&gt;
                        count_change=cntcheck(data_read,current_data,veid,fields,count,count_change)&lt;br /&gt;
&lt;br /&gt;
        if barrier_break and count == False:&lt;br /&gt;
                print barrier_break&lt;br /&gt;
                sys.exit(2)&lt;br /&gt;
        elif count == False:&lt;br /&gt;
                print 'All Beancounters OK'&lt;br /&gt;
                sys.exit(0)&lt;br /&gt;
&lt;br /&gt;
        if count_change and count == True:&lt;br /&gt;
                send_mail(count_change)&lt;br /&gt;
                return current_data&lt;br /&gt;
        elif count == True:&lt;br /&gt;
                return current_data&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# ----- pickle data - read or write&lt;br /&gt;
&lt;br /&gt;
def pickle_data(current_data,action,count,picklefilepath):&lt;br /&gt;
        try:&lt;br /&gt;
                picklefile = None&lt;br /&gt;
                if action == 'write':&lt;br /&gt;
                        if current_data:&lt;br /&gt;
                                picklefile=open(picklefilepath,'w')&lt;br /&gt;
                                pickle.dump(current_data, picklefile)&lt;br /&gt;
                                picklefile.close()&lt;br /&gt;
                                return&lt;br /&gt;
                        else:&lt;br /&gt;
                                print 'current_data is empty: ' + str(current_data)&lt;br /&gt;
                elif action == 'read':&lt;br /&gt;
                        picklefile=open(picklefilepath,'r')&lt;br /&gt;
                        data_read=pickle.load(picklefile)&lt;br /&gt;
                        picklefile.close()&lt;br /&gt;
                        if data_read:&lt;br /&gt;
                                return data_read&lt;br /&gt;
                        else:&lt;br /&gt;
                                print 'DATA_READ IS NONE:' + str(data_read)&lt;br /&gt;
                                return data_read&lt;br /&gt;
        except IOError:&lt;br /&gt;
                current_data = compare_data(beancounter_data,'0',count)&lt;br /&gt;
                picklefile=open(picklefilepath,'w')&lt;br /&gt;
                pickle.dump(current_data,picklefile)&lt;br /&gt;
                picklefile.close()&lt;br /&gt;
&lt;br /&gt;
# ------- print script usage&lt;br /&gt;
&lt;br /&gt;
def usage(prog=&amp;quot;check_beancounter.py&amp;quot;):&lt;br /&gt;
    print &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
check_beancounter.py : Check if failcounters increase or resource-values break barriers or limits&lt;br /&gt;
&lt;br /&gt;
 check_beancounter.py [-tfh]&lt;br /&gt;
&lt;br /&gt;
 -h                  print this message&lt;br /&gt;
&lt;br /&gt;
 -t                  Check if failcnt-values have increased since the last run&lt;br /&gt;
 -f                  Check if current value of a resource is higher than barrier/limit&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
opts=getopt.getopt(sys.argv[1:], 'thf')&lt;br /&gt;
if opts:&lt;br /&gt;
        if opts[0]==[]:&lt;br /&gt;
                usage(); sys.exit(0)&lt;br /&gt;
        elif opts[0][0][0]=='-h':&lt;br /&gt;
                usage(); sys.exit(0)&lt;br /&gt;
        elif opts[0][0][0]=='-t':&lt;br /&gt;
                count=True&lt;br /&gt;
        elif opts[0][0][0]=='-f':&lt;br /&gt;
                count=False&lt;br /&gt;
&lt;br /&gt;
beancounter_data=open('/proc/user_beancounters','r')&lt;br /&gt;
data_read=pickle_data(current_data,'read',count,picklefilepath)&lt;br /&gt;
current_data = compare_data(beancounter_data,data_read,count)&lt;br /&gt;
pickle_data(current_data,'write',count,picklefilepath)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is an alternative script, also written in python:&lt;br /&gt;
[http://www.kbrandt.com/2008/10/openvz-beancounters-nagios-script.html OpenVZ Nagios Bean Counters Script]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Monitoring]]&lt;/div&gt;</summary>
		<author><name>Kbrandt</name></author>
		
	</entry>
</feed>