Advertisement
DAnger1988

SystemInfo

Apr 4th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. #Test writing to file
  2. import platform             #Holds all functions used to retrieve the platform specific information
  3. import getpass              #Holds the getuser() function
  4. import psutil               #Holds all the functions pertaining to cpu_times and system memory
  5.  
  6.  
  7. user = getpass.getuser()                            #This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string.
  8. system = platform.system()                          #Create another variable 'system' and set it equal to the value returned by platform.system() function
  9. node = platform.node()
  10. release = platform.release()
  11. version = platform.version()
  12. machine = platform.machine()
  13. processor = platform.processor()
  14. platform = platform.platform()
  15. f = open('systemInfo.txt', 'w')                     #Open our logfile 'systemInfo.txt' and set the write option
  16. f.write ('Username:  {}\n'.format(user))            #Write out the name of the user that is "logged in" to the systemInfo.txt file.
  17. f.write('System/OS: {}\n'.format(system))           #Write the system/OS name to the systemInfo.txt file.
  18. f.write('Node:      {}\n'.format(node))             #Write the computer's network name (may not be fully qualified!). An empty string is returned if the value cannot be determined and write that string to the systemInfo.txt file.
  19. f.write('Release:   {}\n'.format(release))          #Write the system’s release, e.g. '10' for Windows 10 or 'NT' An empty string is returned if the value cannot be determined. Write the output to the systemInfo.txt file
  20. f.write('Version:   {}\n'.format(version))          #Write the release version, e.g. '10.0.14393' for current version of Windows 10 to the systemInfo.txt file
  21. f.write('Machine:   {}\n'.format(machine))          #Write the machine type, e.g. 'i386' or 'AMD64' to the systemInfo.txt file
  22. f.write('Processor: {}\n'.format(processor))
  23. f.write('Platform:  {}\n\n'.format(platform))
  24.  
  25. #Calling the function psutil.cpu_times() returns a named tuple that looks like this (on my windows machine).
  26. #+scputimes(user=6622.6875, system=1534.6875, idle=68117.65625, interrupt=77.31250095367432, dpc=46.390625)
  27. #+In order to write the values to the systemInfo.txt file I assigned a variable name to each key-value pair in the namedtuple.
  28. scputimes = psutil.cpu_times()                      #Set the variable scputimes equal to the namedtuple psutil.cpu_times, so I can pull out specific key-value pairs.
  29. user = scputimes.user                               #+set the variable name user equal to the value of the namedtuple key user.
  30.                                                     #+Time spent by normal processes executing in user mode; on Linux this also includes guest time
  31.  
  32. scputimes = psutil.cpu_times()                      #Time spent by processes executing in kernel mode
  33. system = scputimes.system
  34.  
  35. scputimes = psutil.cpu_times()                      #Retrieve idle time
  36. idle = scputimes.idle                               #time spent doing nothing
  37.  
  38. scputimes = psutil.cpu_times()                      #Retrieve interrupt time
  39. interrupt = scputimes.interrupt                     #Time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
  40.  
  41. scputimes = psutil.cpu_times()                      #Time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.
  42. dpc = scputimes.dpc
  43.  
  44. f.write('CPU Times \n')
  45.  #Format cpu_time values in hours, minutes, and seconds (down to the 100th of a second) by using floor division on the original value (which is returned by the function in seconds).
  46.  #+To get a whole number for hours, I floor divide the original value by 3600 then use modulus division combined with floor division to get a whole number for minutes, then use modulus division to get a floting point number for seconds.
  47. f.write('Time spent executing in user mode:             {:3.0f} hours, {:2.0f} minute(s), {:2.2f} seconds \n'.format(user//3600, user%3600//60, user%60))
  48. f.write('Time spent executing in kernel mode:           {:3.0f} hours, {:2.0f} minute(s), {:2.2f} seconds \n'.format(system//3600, system%3600//60, system%60))
  49. f.write('Time spent doing nothing:                      {:3.0f} hours, {:2.0f} minute(s), {:2.2f} seconds \n'.format(idle//3600, idle%3600//60, idle%60))
  50. f.write('Time spent servicing hardware interrupts:      {:3.0f} hours, {:2.0f} minute(s), {:2.2f} seconds \n'.format(interrupt//3600, interrupt%3600//60, interrupt%60))
  51. f.write('Time spent servicing deferred procedure calls: {:3.0f} hours, {:2.0f} minute(s), {:2.2f} seconds \n \n'.format(dpc//3600, dpc%3600//60, dpc%60))
  52.  
  53. f.write('Memory Information \n')
  54. #Retrieve the namedtuple returned by virtual_memory() function and set it equal to the variable svem.
  55. svem = psutil.virtual_memory()
  56. f.write('Total physical memory is:       {0:,.2f} MB \n'.format(svem.total/1024/1024))      #Write total physical memory in Megaabytes to systemInfo.txt
  57. f.write('Available physical memory is:   {0:,.2f} MB \n'.format(svem.available/1024/1024))  #Write amount of available memory in Megabytes to systemInfo.txt.
  58. f.write('Memory used:                    {0:,.2f} MB \n'.format(svem.used/1024/1024))       #Memory used, calculated differently depending on the platform and designed for informational purposes only.
  59.                                                                                             #+  (total - free) does not necessarily match used.
  60. f.write('Percentage of memory available: {}% \n \n'.format (svem.percent))
  61. THRESHOLD = 100 * 1024 * 1024       #100MB
  62.  
  63. if svem.available <= THRESHOLD:
  64.     f.write('Warning: Available memory is below 100MB')
  65. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement