Proyecto

General

Perfil

#!/usr/bin/env python
##############################################################################
# -*- coding: utf-8 -*-
# Project:
# Module: puppetlastgroups
# Purpose: Gets info about last sinchronization date via puppet (grouped by classroom) from /var/lib/puppet/yaml/facts certs
# Language: Python 2.5
# Date: 27-Abr-2012.
# Ver: 27-Abr-2012.
# Author: Francisco Mendez Palma
# Manu Mora Gordillo
# Copyright: 2012 - Francisco Mendez Palma <fmendezpalma @no-spam@ gmail.com>
# 2012 - Manuel Mora Gordillo <manuito @ no-spam@ gmail.com>
#
# puppetlastgroups 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 3 of the License, or
# (at your option) any later version.
# puppetlastgroups 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.
#
##############################################################################

import ldap
import sys
import subprocess
import glob
import os

from datetime import datetime, timedelta

def humanize(delta):
days = delta.days
hours = delta.seconds / 3600
minutes = delta.seconds % 3600 / 60
seconds = delta.seconds % 3600 % 60

str = ""
tStr = ""
if days > 0:
if days == 1: tStr = "day"
else: tStr = "days"
str = str + "%s %s" %(days, tStr)
elif hours > 0:
if hours == 1: tStr = "hour"
else: tStr = "hours"
str = str + "%s %s" %(hours, tStr)
elif minutes > 0:
if minutes == 1: tStr = "min"
else: tStr = "mins"
str = str + "%s %s" %(minutes, tStr)
elif seconds > 0:
if seconds == 1: tStr = "sec"
else: tStr = "secs"
str = str + "%s %s" %(seconds, tStr)
return str


# Obtiene lista nombres de maquinas a partir de los certificados almacenados en el servidor, en /var/lib/puppet/yaml/facts
# asi como informacion del periodo que llevan sin sincronizar via puppet.
def gettimes(search):
times = []
out = ''
import pdb
for f in glob.glob("/var/lib/puppet/yaml/facts/*"):
host = f.split('/')[-1].replace('.yaml', '')
last = datetime.now() - datetime.fromtimestamp(os.stat(f)[9])
times.append((humanize(last), host, last))
return times

# Muestra en pantalla la info de la sincronizacion con codigo de colores
def printtimes(times):
RED = '\033[91m'
GREEN = '\033[92m'
ORANGE = '\033[93m'
RESET = '\033[0m'
for time in reversed(sorted(times, key=lambda x: x[2])):
if time[2] < timedelta(hours=24):
print GREEN, "%10s ago: %s" % (time[0], time[1]), RESET
elif time[2] < timedelta(hours=239):
print ORANGE, "%10s ago: %s" % (time[0], time[1]), RESET
else:
print RED, "%10s ago: %s" % (time[0], time[1]), RESET

def formatList(search):
from operator import itemgetter, attrgetter
data=[]
for i in search:
classroom=i[0][0].replace("cn=","").replace(",ou=Group,dc=instituto,dc=extremadura,dc=es","")
try:
members=i[0][1]['memberUid']
except:
pass

tuple = (classroom,members)
data.append(tuple)

tmp=sorted(data, key=itemgetter(0))
return tmp

def formatListLTSP(search):
from operator import itemgetter, attrgetter
data=[]
tmp=[]
servers=search[0][0][1]['nisNetgroupTriple']
for s in servers:
tmp.append(s.replace("(","").replace(",-,-)",""))

tuple = ("LTSP-Servers",tmp)
data.append(tuple)
tmp=sorted(data, key=itemgetter(1))
return tmp


# Rutina principal
def puppetlast(times,search):
import pdb
for c in search:
if c[0]:
times_temp=[]
alumnos=c[1]
curso=c[0]
if len(alumnos)>1:
for a in alumnos:
for t in times:
if (a in t[1]) and (a!=""):
times_temp.append (t)

if len(times_temp)>1:
print "\n- "+curso+" ("+str(len(alumnos))+" elementos) ("+str(len(times_temp))+" certificados)"
printtimes(times_temp)


def ldapSearch(baseDN,filter,retrieveAttributes):
try:
l = ldap.open("ldap")
except ldap.LDAPError, e:
print "Error coneon LDAP"

try:
ldap_result_id = l.search(baseDN+",dc=instituto,dc=extremadura,dc=es", ldap.SCOPE_SUBTREE, filter, retrieveAttributes)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
return result_set
except ldap.LDAPError, e:
print e


grupo=""
if len (sys.argv)==2:
grupo=sys.argv[1]
filter="(&(groupType=school_class)(cn="+grupo+"))"
else:
filter="(&(groupType=school_class))"

search = ldapSearch("ou=Group",filter,["memberUid"])

data = formatList(search)
t=gettimes(data)
puppetlast(t,data)

if grupo=="":
search = ldapSearch("cn=ltsp-server-hosts, ou=Netgroup","cn=*",["nisNetgroupTriple"])
data = formatListLTSP(search)
t=gettimes(data)
puppetlast(t,data)
(2-2/2)