Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Test writing to file
- import platform #Holds all functions used to retrieve the platform specific information
- import getpass #Holds the getuser() function
- import psutil #Holds all the functions pertaining to cpu_times and system memory
- 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.
- system = platform.system() #Create another variable 'system' and set it equal to the value returned by platform.system() function
- node = platform.node()
- release = platform.release()
- version = platform.version()
- machine = platform.machine()
- processor = platform.processor()
- platform = platform.platform()
- f = open('systemInfo.txt', 'w') #Open our logfile 'systemInfo.txt' and set the write option
- f.write ('Username: {}\n'.format(user)) #Write out the name of the user that is "logged in" to the systemInfo.txt file.
- f.write('System/OS: {}\n'.format(system)) #Write the system/OS name to the systemInfo.txt file.
- 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.
- 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
- 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
- f.write('Machine: {}\n'.format(machine)) #Write the machine type, e.g. 'i386' or 'AMD64' to the systemInfo.txt file
- f.write('Processor: {}\n'.format(processor))
- f.write('Platform: {}\n\n'.format(platform))
- #Calling the function psutil.cpu_times() returns a named tuple that looks like this (on my windows machine).
- #+scputimes(user=6622.6875, system=1534.6875, idle=68117.65625, interrupt=77.31250095367432, dpc=46.390625)
- #+In order to write the values to the systemInfo.txt file I assigned a variable name to each key-value pair in the namedtuple.
- scputimes = psutil.cpu_times() #Set the variable scputimes equal to the namedtuple psutil.cpu_times, so I can pull out specific key-value pairs.
- user = scputimes.user #+set the variable name user equal to the value of the namedtuple key user.
- #+Time spent by normal processes executing in user mode; on Linux this also includes guest time
- scputimes = psutil.cpu_times() #Time spent by processes executing in kernel mode
- system = scputimes.system
- scputimes = psutil.cpu_times() #Retrieve idle time
- idle = scputimes.idle #time spent doing nothing
- scputimes = psutil.cpu_times() #Retrieve interrupt time
- interrupt = scputimes.interrupt #Time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
- scputimes = psutil.cpu_times() #Time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.
- dpc = scputimes.dpc
- f.write('CPU Times \n')
- #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).
- #+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.
- 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))
- 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))
- 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))
- 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))
- 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))
- f.write('Memory Information \n')
- #Retrieve the namedtuple returned by virtual_memory() function and set it equal to the variable svem.
- svem = psutil.virtual_memory()
- f.write('Total physical memory is: {0:,.2f} MB \n'.format(svem.total/1024/1024)) #Write total physical memory in Megaabytes to systemInfo.txt
- 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.
- 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.
- #+ (total - free) does not necessarily match used.
- f.write('Percentage of memory available: {}% \n \n'.format (svem.percent))
- THRESHOLD = 100 * 1024 * 1024 #100MB
- if svem.available <= THRESHOLD:
- f.write('Warning: Available memory is below 100MB')
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement