Proyecto

General

Perfil

#!/usr/bin/env python
#########################################################################################################
# -*- coding: utf-8 -*-
# Project: puppetlast
# Module: puppetlast
# Purpose: Gets info about last sinchronization date via puppet from /var/lib/puppet/yaml/facts certs
# Language: Python 2.5
# Date: 4-May-2012.
# Ver: 4-May-2012.
# Author: https://github.com/gregarmer/puppetlast
#########################################################################################################

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


times = []
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))

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


print "\n Total certificados: "+str(len(times))
(1-1/2)