Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. import datetime
  3. import psutil
  4. import math
  5.  
  6. def get_uptime():
  7.     """
  8.    Returns the boot time of the system, formated as days, hours, minutes and seconds
  9.    psutil.boot_time() returns the boot time in epoch seconds, so we subtract that
  10.    from the current time in epoch seconds
  11.  
  12.    We then convert back using divmod, which does modulus on the first argument by the second,
  13.    then returns a tuple of the modulus followed by the remainder
  14.    """
  15.  
  16.     seconds = datetime.datetime.now().timestamp() - psutil.boot_time()
  17.     m, s = divmod(seconds, 60)
  18.     h, m = divmod(m, 60)
  19.     d, h = divmod(h, 24)
  20.     return '{} days, {} hours, {} minutes, {} seconds'.format(int(d), int(h), int(m), int(s))
  21.  
  22. def get_cpu_load():
  23.     """
  24.    We use the argument 2 here, which is the interval the psutil.cpu_percent() checks for
  25.    This is not too low as to return an incorrect number, but not too high as it is a
  26.    blocking process
  27.    """
  28.    
  29.     return psutil.cpu_percent(2)
  30.  
  31. def get_free_memory():
  32.     """
  33.    psutil.virtual_memory() returns a named_tuple full of information about virtual memory
  34.    For the purposes of this script, we are only interested in free memory, which is at [4]
  35.    """
  36.     memory = psutil.virtual_memory()
  37.     free_memory = memory[4] / 1024 ** 2
  38.     return math.floor(free_memory)
  39.    
  40. def get_free_storage():
  41.     """
  42.    This functions very similarly to get_free_memory above, only that
  43.    psutil.disk_usage('/') provides info about the root filesystem instead
  44.    of virtual memory.
  45.    In this instance, we are interested only in free space, which is at [2]
  46.    On windows this reports free space on the OS drive
  47.    """
  48.     storage = psutil.disk_usage('/')
  49.     free_space = storage[2] / 1024 ** 2
  50.     return math.floor(free_space)
  51.    
  52. def get_net_info():
  53.     netInfo = psutil.net_io_counters()
  54.  
  55.     mbSent = netInfo[0] / 1024 ** 2
  56.     mbRec = netInfo[1] / 1024 ** 2
  57.  
  58.     return math.floor(mbSent), math.floor(mbRec)
  59.    
  60. print(get_uptime())
  61. print(get_cpu_load())
  62. print(get_free_memory())
  63. print(get_free_storage())
  64. print(get_net_info())
  65.  
  66.  
  67. username = "bancbrxq"
  68. password = "UFLIsH2raojY"
  69. client_id = "System Test"
  70. clean_session = False
  71.  
  72. mqttc = mqtt.Client(client_id, clean_session)
  73. mqttc.username_pw_set(username, password)
  74.  
  75. mqttc.connect("m21.cloudmqtt.com", 17472)
  76. mqttc.loop_start()
  77.  
  78.  
  79. #This stores a datetime object in CT, which we then format
  80. ct = datetime.datetime.now()
  81. current_time = ct.strftime("%D - %H:%M:%S")
  82.  
  83. uptime = get_uptime()
  84. cpu_load = get_cpu_load()
  85. free_mem = get_free_memory()
  86. free_storage = get_free_storage()
  87. mb_sent, mb_received = get_net_info()
  88.  
  89.  
  90. mqttc.publish("systemhealth/uptime", uptime, 1, True)
  91. mqttc.publish("systemhealth/cpuload", cpu_load, 1, True)
  92. mqttc.publish("systemhealth/freememory", free_mem, 1, True)
  93. mqttc.publish("systemhealth/freestorage", free_storage, 1, True)
  94. mqttc.publish("systemhealth/netsent", mb_sent, 1, True)
  95. mqttc.publish("systemhealth/netreceived", mb_received, 1, True)
  96. mqttc.publish("systemhealth/lastupdate", current_time, 1, True)
  97. mqttc.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement