Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #! /usr/bin/python3
  2. import calendar
  3. import re
  4. import sys
  5. from dateutil.parser import parse
  6. import datetime
  7.  
  8. def write_csv(downtimes, output):
  9. with open(output, 'w+') as f:
  10. i = 1
  11. for month in sorted(downtimes.keys()):
  12. f.write('Start,End,Downtime in seconds,Downtime,Planned?,Comment\n')
  13. i = i + 1
  14. first_row = i
  15. total_sec = downtimes[month]['days'] * 24 * 60 * 60
  16. for begin, end in downtimes[month]['downtimes']:
  17. sec = (end - begin).total_seconds()
  18. f.write('"=""%s""","=""%s""",%i,"=""%s""",No,\n' %
  19. (begin,
  20. end,
  21. sec,
  22. str(datetime.timedelta(seconds=sec)),
  23. ))
  24. i = i + 1
  25. last_row = i - 1
  26. f.write('\nMonth,Downtime in seconds,Downtime in minutes,SLA\n')
  27. i = i + 2
  28. f.write('"=""%s""","=SUMIF(E%i:E%i,""No"",C%i:C%i)","=TEXT(TIME(INT(B%i/3600),MOD(INT(B%i/60),60),MOD(B%i, 60)), ""h:mm"")","=100-(((B%i)/%i)*100)"\n\n\n\n\n' % (month, first_row, last_row, first_row, last_row, i, i, i, i, total_sec))
  29. i = i + 5
  30.  
  31. def append_downtime(downtimes, begin, end):
  32. month = '%i/%02d' % (begin.year, begin.month)
  33. if month not in downtimes:
  34. _, days = calendar.monthrange(begin.year, begin.month)
  35. downtimes[month] = {'days': days, 'downtimes': []}
  36. downtimes[month]['downtimes'].append((begin, end))
  37. return downtimes
  38.  
  39.  
  40. def parse_log(logfile):
  41. with open(logfile) as f:
  42. downtimes = {}
  43. for line in f:
  44. if 'Unavailable' in line:
  45. match = re.search(r'Unavailable ([0-9.]+) ([0-9:]+) - ([0-9.]+) ([0-9:]+)', line)
  46. if match:
  47. begin = parse('%s %s' % (match.group(1), match.group(2)), dayfirst=True)
  48. end = parse('%s %s' % (match.group(3), match.group(4)), dayfirst=True)
  49. if begin.year == end.year and \
  50. begin.month == end.month:
  51. downtimes = append_downtime(downtimes, begin, end)
  52. else:
  53. _, last_day = calendar.monthrange(begin.year, begin.month)
  54. end_of_month = parse('%s.%s.%s 23:59:59' % (last_day, begin.month, begin.year), dayfirst=True)
  55. beginning_of_month = parse('01.%s.%s 00:00:00' % (end.month, end.year), dayfirst=True)
  56. downtimes = append_downtime(downtimes, begin, end_of_month)
  57. downtimes = append_downtime(downtimes, beginning_of_month, end)
  58. return downtimes
  59.  
  60. def main():
  61. if len(sys.argv) < 3:
  62. print('Usage: ./%s available.log destination.csv' % sys.argv[0])
  63. return 1
  64. downtimes = parse_log(sys.argv[1])
  65. write_csv(downtimes, sys.argv[2])
  66.  
  67. if __name__ == "__main__":
  68. sys.exit(main())
Add Comment
Please, Sign In to add comment