Proyecto

General

Perfil

321 manumora
#!/usr/bin/env python
##############################################################################
# -*- coding: utf-8 -*-
# Project:
# Module: puppetlastgroups
334 manumora
# Purpose: Gets info about last sinchronization date via puppet (grouped) from /var/lib/puppet/yaml/facts certs
321 manumora
# 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

334 manumora
total_count=0
all_hosts=[]
all_certs=[]

321 manumora
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.
334 manumora
def gettimes():
321 manumora
times = []
334 manumora
global all_certs
321 manumora
out = ''
import pdb
for f in glob.glob("/var/lib/puppet/yaml/facts/*"):
host = f.split('/')[-1].replace('.yaml', '')
334 manumora
all_certs.append(host)
321 manumora
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):
334 manumora
global all_hosts

321 manumora
RED = '\033[91m'
GREEN = '\033[92m'
ORANGE = '\033[93m'
RESET = '\033[0m'
for time in reversed(sorted(times, key=lambda x: x[2])):
334 manumora
all_hosts.append(time[1])
321 manumora
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

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

334 manumora
tuple = (head,tmp)
323 manumora
data.append(tuple)
tmp=sorted(data, key=itemgetter(1))
return tmp


321 manumora
# Rutina principal
def puppetlast(times,search):
import pdb
334 manumora
global total_count

321 manumora
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:
323 manumora
print "\n- "+curso+" ("+str(len(alumnos))+" elementos) ("+str(len(times_temp))+" certificados)"
334 manumora
total_count=total_count+len(times_temp)
321 manumora
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


334 manumora
def cmpLists(list1, list2):
onlyInList1 = set(list1).difference(set(list2))
onlyInList2 = set(list2).difference(set(list1))
inTwoLists = set(list1) & set(list2)

return { 'onlyInList1':onlyInList1, 'onlyInList2':onlyInList2, 'inTwoLists':inTwoLists }


321 manumora
grupo=""
334 manumora
t=gettimes()

321 manumora
if len (sys.argv)==2:
323 manumora
grupo=sys.argv[1]
filter="(&(groupType=school_class)(cn="+grupo+"))"
321 manumora
else:
323 manumora
filter="(&(groupType=school_class))"
321 manumora
search = ldapSearch("ou=Group",filter,["memberUid"])

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

323 manumora
if grupo=="":
334 manumora
# Servidores de aula
323 manumora
search = ldapSearch("cn=ltsp-server-hosts, ou=Netgroup","cn=*",["nisNetgroupTriple"])
334 manumora
data = formatOtherList(search,"LTSP-Servers")
323 manumora
puppetlast(t,data)
334 manumora
# Portatiles de profesores. Debera tener la rama Netgroup->laptop-hosts
try:
search = ldapSearch("cn=workstation-hosts, ou=Netgroup","cn=*",["nisNetgroupTriple"])
data = formatOtherList(search,"Workstations")
puppetlast(t,data)
except:
pass


# Portatiles de profesores. Debera tener la rama Netgroup->laptop-hosts
try:
search = ldapSearch("cn=laptop-hosts, ou=Netgroup","cn=*",["nisNetgroupTriple"])
data = formatOtherList(search,"Portatiles profesores")
puppetlast(t,data)
except:
pass

# Equipos desconocidos
cmp=cmpLists(all_hosts, all_certs)
unknown=[]
unknown.append(("Desconocidos",cmp["onlyInList2"]))
puppetlast(t,unknown)

print "\nTotal certificados: "+str(total_count)