Advertisement
Guest User

Untitled

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