Advertisement
Guest User

Untitled

a guest
Jul 16th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # +------------------------------------------------------------------+
  4. # |             ____ _               _        __  __ _  __           |
  5. # |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
  6. # |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
  7. # |           | |___| | | |  __/ (__|   <    | |  | | . \            |
  8. # |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
  9. # |                                                                  |
  10. # | Copyright Mathias Kettner 2018             mk@mathias-kettner.de |
  11. # +------------------------------------------------------------------+
  12. #
  13. # This file is part of Check_MK.
  14. # The official homepage is at http://mathias-kettner.de/check_mk.
  15. #
  16. # check_mk is free software;  you can redistribute it and/or modify it
  17. # under the  terms of the  GNU General Public License  as published by
  18. # the Free Software Foundation in version 2.  check_mk is  distributed
  19. # in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
  20. # out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
  21. # PARTICULAR PURPOSE. See the  GNU General Public License for more de-
  22. # tails. You should have  received  a copy of the  GNU  General Public
  23. # License along with GNU Make; see the file  COPYING.  If  not,  write
  24. # to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
  25. # Boston, MA 02110-1301 USA.
  26.  
  27. # <<<win_license>>>
  28. #
  29. # Name: Windows(R) 7, Enterprise edition
  30. # Description: Windows Operating System - Windows(R) 7, TIMEBASED_EVAL channel
  31. # Partial Product Key: JCDDG
  32. # License Status: Initial grace period
  33. # Time remaining: 11820 minute(s) (8 day(s))
  34.  
  35. factory_settings['windows_license_default_levels'] = {
  36.     'status': ['Licensed', 'Initial grace period'],
  37.     'status2': ['Lizenziert', 'Initial grace period'],
  38.     'expiration_time': (14 * 24 * 60 * 60, 7 * 24 * 60 * 60),
  39. }
  40.  
  41.  
  42. def parse_win_license(info):
  43.     parsed = {}
  44.     for line in info:
  45.         if len(line) == 0:
  46.             continue
  47.  
  48.         if  line[0] == 'License':
  49.             parsed['License'] = ' '.join(line[2:])
  50. ##
  51.         if line[0] == 'Lizenzstatus:':
  52.             parsed['Lizenzstatus:'] = ' '.join(line[1:])
  53. ##
  54.         # Depending on Windows version this variable reads
  55.         # Time remaining or Volume activation expiration or is not present
  56.  
  57.         if line[0] in ['Time', 'Volume']:
  58.             expiration = ' '.join(line).split(':')[1].strip()
  59.             parsed["expiration"] = expiration
  60.             time_left_re = regex(r'(\d+) minute')
  61.             time_left = int(time_left_re.search(expiration).group(1)) * 60
  62.             parsed['expiration_time'] = time_left
  63. #    return parsed
  64. ##
  65.         if line[0] in ['Time', 'Ablauf']:
  66.             expiration = ' '.join(line).split(':')[1].strip()
  67.             parsed["Volumenaktivierung"] = expiration
  68.             time_left_re = regex(r'(\d+) Minute')
  69.             time_left = int(time_left_re.search(expiration).group(1)) * 60
  70.             parsed['expiration_time'] = time_left
  71.     return parsed
  72.  
  73.  
  74. def inventory_win_license(parsed):
  75.     if 'License' in parsed:
  76.         return [(None, {})]
  77.  
  78.     if 'Lizenzstatus:' in parsed:
  79.         return [(None, {})]
  80.  
  81.  
  82.  
  83. def check_win_license(_item, params, parsed):
  84.  
  85.     if 'License' in parsed:
  86.       sw_license = parsed.get('License', None)
  87.  
  88.     if 'Lizenzstatus:' in parsed:
  89.       sw_license = parsed.get('Lizenzstatus:', None)
  90.  
  91.     if not sw_license:
  92.         return
  93.  
  94.     message = "Software is %s" % sw_license
  95.    
  96.     license_state = 0 if sw_license in params['status'] else 0 if sw_license in params['status2'] else 2
  97.  
  98.     if license_state:
  99.         message += " Required: " + ' '.join(params['status'])
  100.  
  101.     yield license_state, message
  102.  
  103.     time_left = parsed.get('expiration_time', None)
  104.  
  105.     if not time_left:
  106.         return
  107.  
  108.     time_message = "License will expire in %s" % get_age_human_readable(time_left)
  109.  
  110.     warn, crit = params['expiration_time']
  111.  
  112.     time_state = 0
  113.  
  114.     if time_left < crit:
  115.         time_state = 2
  116.     elif time_left < warn:
  117.         time_state = 1
  118.  
  119.     if time_state:
  120.         time_message += " (warn/crit at %s/%s)" % tuple(map(get_age_human_readable, (warn, crit)))
  121.  
  122.     yield time_state, time_message
  123.  
  124.  
  125. check_info['win_license'] = {
  126.     'service_description': "Windows License",
  127.     'parse_function': parse_win_license,
  128.     'inventory_function': inventory_win_license,
  129.     'check_function': check_win_license,
  130.     'group': 'win_license',
  131.     'default_levels_variable': 'windows_license_default_levels',
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement