[munin/innd_total] On essaie d'eviter un peu mieux le timeout
Le graphe avait beaucoup de trous, il en a deja presque plus ... darcs-hash:20100219090906-ddb99-cb9c395a6286a5d527920725ec33bc7431850e40.gz
This commit is contained in:
parent
3df0b90be9
commit
5329f5a1e7
1 changed files with 83 additions and 42 deletions
|
@ -20,11 +20,16 @@
|
||||||
"""Plugin munin pour innd :: donne le nombre total de posts par newsgroup."""
|
"""Plugin munin pour innd :: donne le nombre total de posts par newsgroup."""
|
||||||
|
|
||||||
## Importations
|
## Importations
|
||||||
import socket, sys, time
|
import socket, sys, threading, time
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
# Fichier de cache pour stocker les données entre deux lancements
|
||||||
cache_file = "/tmp/munin_innd_total_cache"
|
cache_file = "/tmp/munin_innd_total_cache"
|
||||||
|
# Effectuer le compte exact du nombre de messages (beaucoup plus lent)
|
||||||
|
# sinon, utilise le compte approximatif envoyé par le serveur
|
||||||
|
exact_count = False
|
||||||
|
# Nombre de messages minimal pour être affiché
|
||||||
|
min_messages = 100
|
||||||
|
|
||||||
def recvlines(sock):
|
def recvlines(sock):
|
||||||
buff = sock.recv(4096)
|
buff = sock.recv(4096)
|
||||||
|
@ -32,22 +37,24 @@ def recvlines(sock):
|
||||||
buff += sock.recv(4096)
|
buff += sock.recv(4096)
|
||||||
return buff.split('\r')[:-2]
|
return buff.split('\r')[:-2]
|
||||||
|
|
||||||
## Connexion
|
|
||||||
s = socket.socket()
|
|
||||||
s.connect(('localhost', 119))
|
|
||||||
|
|
||||||
## Mise en mode reader (pour utiliser 'listgroup')
|
def get_newsgroups_data():
|
||||||
s.send("mode reader\r\n")
|
## Connexion
|
||||||
|
s = socket.socket()
|
||||||
|
s.connect(('localhost', 119))
|
||||||
|
|
||||||
# Petit "hack" en attendant d'utiliser la nntplib
|
## Mise en mode reader (pour utiliser 'listgroup')
|
||||||
time.sleep(0.5)
|
s.send("mode reader\r\n")
|
||||||
|
|
||||||
s.recv(4096)
|
# Petit "hack" en attendant d'utiliser la nntplib
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
## Récupération des newsgroups et du nombre de posts
|
s.recv(4096)
|
||||||
s.send("list\r\n")
|
|
||||||
newsgroups = []
|
## Récupération des newsgroups et du nombre de posts
|
||||||
for l in recvlines(s)[1:]:
|
s.send("list\r\n")
|
||||||
|
newsgroups = []
|
||||||
|
for l in recvlines(s)[1:]:
|
||||||
name = l.split()[0]
|
name = l.split()[0]
|
||||||
|
|
||||||
# Conversion du nom vers un format accepté par munin
|
# Conversion du nom vers un format accepté par munin
|
||||||
|
@ -58,26 +65,36 @@ for l in recvlines(s)[1:]:
|
||||||
cname = cname.replace('-', '_')
|
cname = cname.replace('-', '_')
|
||||||
|
|
||||||
# Récupération du nombre de posts
|
# Récupération du nombre de posts
|
||||||
|
if exact_count:
|
||||||
s.send('listgroup %s\r\n' % name)
|
s.send('listgroup %s\r\n' % name)
|
||||||
val = len(recvlines(s))
|
val = len(recvlines(s))
|
||||||
|
else:
|
||||||
|
s.send('group %s\r\n' % name)
|
||||||
|
val = int(s.recv(4096).split()[1])
|
||||||
|
|
||||||
# On ne garde pas les newsgroups presque vides
|
# On ne garde pas les newsgroups presque vides
|
||||||
if val > 5:
|
if val > min_messages:
|
||||||
newsgroups.append((name, cname, val))
|
newsgroups.append((name, cname, val))
|
||||||
|
|
||||||
## On garde un cache si jamais le serveur nous bounce
|
## On garde un cache si jamais le serveur nous bounce
|
||||||
if len(newsgroups) > 0:
|
if len(newsgroups) > 0:
|
||||||
|
try:
|
||||||
f = open(cache_file, 'w')
|
f = open(cache_file, 'w')
|
||||||
for ngdata in newsgroups:
|
for ngdata in newsgroups:
|
||||||
f.write('%s,%s,%d\n' % ngdata)
|
f.write('%s,%s,%d\n' % ngdata)
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
except:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
for line in open(cache_file, 'r'):
|
for line in open(cache_file, 'r'):
|
||||||
newsgroups.append(line.strip().split(','))
|
newsgroups.append(line.strip().split(','))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
return newsgroups
|
||||||
|
|
||||||
|
|
||||||
## Sortie
|
## Sortie
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == 'config':
|
if len(sys.argv) > 1 and sys.argv[1] == 'config':
|
||||||
# Configuration du graphe
|
# Configuration du graphe
|
||||||
|
@ -87,18 +104,42 @@ if len(sys.argv) > 1 and sys.argv[1] == 'config':
|
||||||
print 'graph_vlabel posts'
|
print 'graph_vlabel posts'
|
||||||
print 'graph_category News'
|
print 'graph_category News'
|
||||||
print 'graph_info Nombre de posts pour chaque newsgroup'
|
print 'graph_info Nombre de posts pour chaque newsgroup'
|
||||||
(ng0, cng0, val0) = newsgroups[0]
|
rnewsgroups = get_newsgroups_data()
|
||||||
|
(ng0, cng0, val0) = rnewsgroups[0]
|
||||||
print '%s.label %s' % (cng0, ng0)
|
print '%s.label %s' % (cng0, ng0)
|
||||||
print '%s.draw AREA' % cng0
|
print '%s.draw AREA' % cng0
|
||||||
for (ng, cng, val) in newsgroups[1:]:
|
for (ng, cng, val) in rnewsgroups[1:]:
|
||||||
print '%s.label %s' % (cng, ng)
|
print '%s.label %s' % (cng, ng)
|
||||||
print '%s.draw STACK' % cng
|
print '%s.draw STACK' % cng
|
||||||
print 'total.label Total'
|
print 'total.label Total'
|
||||||
else:
|
else:
|
||||||
|
# On lance un thread pour éviter les timeouts
|
||||||
|
finished = threading.Event()
|
||||||
|
|
||||||
|
def thread_gnd(return_newsgroups, finished):
|
||||||
|
return_newsgroups.extend(get_newsgroups_data())
|
||||||
|
finished.set()
|
||||||
|
|
||||||
|
rnewsgroups = []
|
||||||
|
t = threading.Thread(target=thread_gnd, args=(rnewsgroups, finished))
|
||||||
|
t.daemon = True
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
# On laisse 30 secondes pour que le thread finisse
|
||||||
|
finished.wait(30)
|
||||||
|
if finished.isSet():
|
||||||
|
fnewsgroups = rnewsgroups
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
for line in open(cache_file, 'r'):
|
||||||
|
fnewsgroups.append(line.strip().split(','))
|
||||||
|
except:
|
||||||
|
fnewsgroups = []
|
||||||
|
|
||||||
# Envoi des valeurs
|
# Envoi des valeurs
|
||||||
lines = []
|
lines = []
|
||||||
total = 0
|
total = 0
|
||||||
for (ng, cng, val) in newsgroups:
|
for (ng, cng, val) in fnewsgroups:
|
||||||
total += val
|
total += val
|
||||||
print '%s.value %d' % (cng, val)
|
print '%s.value %d' % (cng, val)
|
||||||
print 'total.value %d' % total
|
print 'total.value %d' % total
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue