SHARE
TWEET

System Monitor with Ubidots

a guest Sep 18th, 2013 432 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. """
  3. System Monitoring Statistics via Ubidots
  4.  
  5. This script sends CPU, Memory, and Disk Usage statistics to Ubidots for
  6. visualization and insights. This script is cross platform and will work on
  7. Windows, Linux, and OS X.
  8.  
  9. The data source in Ubidots is named after the system"s hostname, and will
  10. be created if it does not exist. The disk usage statistic corresponds to
  11. the first mounted physical partiton and the CPU usage statistics is
  12. the average over all CPU's.
  13. """
  14.  
  15. __author__ = "Daniel da Silva"
  16.  
  17. from socket import gethostname
  18. from sys import argv
  19. import psutil
  20. import ubidots
  21.  
  22.  
  23. def get_var_by_name(var_name, ds):
  24.     """Search for a variable in a datasource. If found, returns the variable.
  25.    If not found, returns None."""
  26.     for var in ds.get_variables():
  27.  
  28.         if var.name == var_name:
  29.             return var
  30.  
  31.     return None
  32.  
  33.  
  34. def main():
  35.     """Main routine for the script."""
  36.     if len(argv) != 2:
  37.         print "Usage: %s API_KEY" % argv[0]
  38.         return
  39.  
  40.     api = ubidots.ApiClient(argv[1])
  41.    
  42.     # Search for a data source with name matching this the desired
  43.     # name of our datasource. If it doesn"t exist, create it.
  44.     hostname = gethostname() + " Monitor"
  45.     ds = None
  46.  
  47.     for cur_ds in api.get_datasources():
  48.         if cur_ds.name == hostname:
  49.             ds = cur_ds
  50.             break
  51.  
  52.     if ds is None:
  53.         ds = api.create_datasource({"name": hostname})
  54.  
  55.     # Search for variables in the data source with names matching
  56.     # the ones we will update. If they don"t exist, create them.
  57.     var_cpu = get_var_by_name("cpu_percent", ds)
  58.     var_mem = get_var_by_name("mem_percent", ds)
  59.     var_disk = get_var_by_name("disk_percent", ds)
  60.    
  61.     if var_cpu is None:
  62.         var_cpu = ds.create_variable({"name": "cpu_percent", "unit": "%"})
  63.  
  64.     if var_mem is None:
  65.         var_mem = ds.create_variable({"name": "mem_percent", "unit": "%"})
  66.  
  67.     if var_disk is None:
  68.         var_disk = ds.create_variable({"name": "disk_percent", "unit": "%"})
  69.  
  70.     # Utilize the psutil module to send values to Ubidots.
  71.     var_cpu.save_value({"value": psutil.cpu_percent(interval=1)})
  72.     var_mem.save_value({"value": psutil.virtual_memory().percent})
  73.     var_disk.save_value({"value": psutil.disk_usage(psutil.disk_partitions(all=False)[0].mountpoint).percent})
  74.  
  75.                        
  76.  
  77. if __name__ == "__main__":
  78.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top