|
#!/usr/bin/env python
|
|
##############################################################################
|
|
# -*- coding: utf-8 -*-
|
|
# Project:
|
|
# Module: puppetlastgroups
|
|
# Purpose: Gets info about last sinchronization date via puppet (grouped) 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
|
|
|
|
total_count=0
|
|
all_hosts=[]
|
|
all_certs=[]
|
|
|
|
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():
|
|
times = []
|
|
global all_certs
|
|
out = ''
|
|
import pdb
|
|
for f in glob.glob("/var/lib/puppet/yaml/facts/*"):
|
|
host = f.split('/')[-1].replace('.yaml', '')
|
|
all_certs.append(host)
|
|
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):
|
|
global all_hosts
|
|
|
|
RED = '\033[91m'
|
|
GREEN = '\033[92m'
|
|
ORANGE = '\033[93m'
|
|
RESET = '\033[0m'
|
|
for time in reversed(sorted(times, key=lambda x: x[2])):
|
|
all_hosts.append(time[1])
|
|
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 formatOtherList(search,head):
|
|
from operator import itemgetter, attrgetter
|
|
data=[]
|
|
tmp=[]
|
|
servers=search[0][0][1]['nisNetgroupTriple']
|
|
for s in servers:
|
|
tmp.append(s.replace("(","").replace(",-,-)",""))
|
|
|
|
tuple = (head,tmp)
|
|
data.append(tuple)
|
|
tmp=sorted(data, key=itemgetter(1))
|
|
return tmp
|
|
|
|
|
|
# Rutina principal
|
|
def puppetlast(times,search):
|
|
import pdb
|
|
global total_count
|
|
RED = '\033[91m'
|
|
RESET = '\033[0m'
|
|
|
|
for c in search:
|
|
if c[0]:
|
|
times_temp=[]
|
|
# Eliminar tuplas vias
|
|
alumnos=[x for x in c[1] if x]
|
|
curso=c[0]
|
|
without_cert=[]
|
|
|
|
if len(alumnos)>1:
|
|
for a in alumnos:
|
|
cert_exists=False
|
|
for t in times:
|
|
if (a==t[1].split(".")[0]) and (a!=""):
|
|
times_temp.append (t)
|
|
cert_exists=True
|
|
if not cert_exists:
|
|
without_cert.append(a)
|
|
|
|
if len(times_temp)>1:
|
|
print "\n- "+curso+" ("+str(len(alumnos))+" elementos) ("+str(len(times_temp))+" certificados)"
|
|
if len(without_cert)>1:
|
|
for w in without_cert:
|
|
if w!="":
|
|
print RED, "Sin certificado: "+w, RESET
|
|
|
|
total_count=total_count+len(times_temp)
|
|
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
|
|
|
|
|
|
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 }
|
|
|
|
|
|
grupo=""
|
|
t=gettimes()
|
|
|
|
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)
|
|
puppetlast(t,data)
|
|
|
|
if grupo=="":
|
|
# Servidores de aula
|
|
search = ldapSearch("cn=ltsp-server-hosts, ou=Netgroup","cn=*",["nisNetgroupTriple"])
|
|
data = formatOtherList(search,"LTSP-Servers")
|
|
puppetlast(t,data)
|
|
|
|
# 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)
|
|
list=[]
|
|
for c in cmp["onlyInList2"]:
|
|
list.append(c.split(".")[0])
|
|
unknown=[]
|
|
unknown.append(("No registrados en LDAP (con certificado)",list))
|
|
puppetlast(t,unknown)
|
|
|
|
print "\nTotal certificados: "+str(total_count)
|