def UnitConversion(aFltValue, valUnit): try: unitNotation = {"bytes": "B", "kilobytes": "kB"}[valUnit] return lib_util.AddSIUnit(aFltValue, unitNotation) except KeyError: pass # Special case needing a conversion. Jeefie. if valUnit == "100 nanoseconds": return lib_util.AddSIUnit(float(aFltValue) / 10, "ms") # Unknown unit. return lib_util.AddSIUnit(aFltValue, valUnit)
def AddStatNode( grph, filNode, infoStat ): # st_size: size of file, in bytes. The SI unit is mentioned. sizUnit = lib_util.AddSIUnit(infoStat.st_size, "B") grph.add( ( filNode, pc.property_file_size, lib_common.NodeLiteral(sizUnit) ) ) grph.add( ( filNode, pc.property_last_access, IntToDateLiteral(infoStat.st_atime) ) ) grph.add( ( filNode, pc.property_last_change, IntToDateLiteral(infoStat.st_mtime) ) ) grph.add( ( filNode, pc.property_last_metadata_change, IntToDateLiteral(infoStat.st_ctime) ) )
def AddStatNode(grph, file_node, info_stat): """Adds to the node of a file some information taken from a call to stat().""" # st_size: size of file, in bytes. The SI unit is mentioned. siz_unit = lib_util.AddSIUnit(info_stat.st_size, "B") grph.add((file_node, pc.property_file_size, lib_util.NodeLiteral(siz_unit))) grph.add((file_node, pc.property_last_access, _int_to_date_literal(info_stat.st_atime))) grph.add((file_node, pc.property_last_change, _int_to_date_literal(info_stat.st_mtime))) grph.add((file_node, pc.property_last_metadata_change, _int_to_date_literal(info_stat.st_ctime)))
def DisplayMappedProcesses(grph, file_name): """This displays all processes mapping a given filename. This simply iterates on processes, then on mapped files of each process. This is not very efficient but there is no other way.""" grph.add((lib_common.nodeMachine, pc.property_hostname, lib_util.NodeLiteral(lib_util.currentHostname))) # This is also a file mapped into memory. uri_mapped_file = lib_uris.gUriGen.FileUri(file_name) uri_mem_map = None try: statinfo = os.stat(file_name) except Exception as exc: grph.add((uri_mapped_file, lib_common.MakeProp("Error"), lib_util.NodeLiteral(str(exc)))) return file_size = lib_util.AddSIUnit(statinfo.st_size, "B") grph.add((uri_mapped_file, pc.property_file_size, lib_util.NodeLiteral(file_size))) prop_memory_rss = lib_common.MakeProp("Resident Set Size") for proc in psutil.process_iter(): pid = proc.pid try: all_maps = proc.memory_maps() except Exception as exc: # Probably psutil.AccessDenied continue for the_map in all_maps: # This, because all Windows paths are "standardized" by us. same_fil = lib_util.standardized_file_path( the_map.path) == lib_util.standardized_file_path(file_name) if same_fil: # Maybe this is the first mapping we have found. if uri_mem_map == None: uri_mem_map = lib_uris.gUriGen.MemMapUri(file_name) grph.add( (uri_mapped_file, pc.property_mapped, uri_mem_map)) node_process = lib_uris.gUriGen.PidUri(pid) # The property is reversed because of display. grph.add((uri_mem_map, pc.property_memmap, node_process)) grph.add( (node_process, pc.property_pid, lib_util.NodeLiteral(pid))) # Displays the RSS only if different from the file size. if the_map.rss != statinfo.st_size: grph.add((node_process, prop_memory_rss, lib_util.NodeLiteral(the_map.rss)))
def Main(): cgiEnv = lib_common.CgiEnv(can_process_remote=True) pid = int(cgiEnv.GetId()) machineName = cgiEnv.GetHost() grph = cgiEnv.GetGraph() if (machineName == lib_util.currentHostname) or (not machineName): #machName_or_None = None serverBox = lib_common.gUriGen else: #machName_or_None = machineName serverBox = lib_common.RemoteBox(machineName) node_process = serverBox.PidUri(pid) cnnct = lib_wmi.WmiConnect(machineName, "/root/cimv2") # lstProcs = cnnct.Win32_Process(Handle=pid) # This also works when selecting from class Win32_Process. lstProcs = cnnct.CIM_Process(Handle=pid) # instance of Win32_Process # { # Caption = "sqlwriter.exe"; # CreationClassName = "Win32_Process"; # CreationDate = "20161215105057.836987+000"; # CSCreationClassName = "Win32_ComputerSystem"; # CSName = "TITI"; # Description = "sqlwriter.exe"; # Handle = "1908"; # HandleCount = 101; # KernelModeTime = "625000"; # Name = "sqlwriter.exe"; # OSCreationClassName = "Win32_OperatingSystem"; # OSName = "Microsoft Windows 8.1|C:\\Windows|\\Device\\Harddisk0\\Partition4"; # OtherOperationCount = "151"; # OtherTransferCount = "1316"; # PageFaults = 3735; # PageFileUsage = 1508; # ParentProcessId = 624; # PeakPageFileUsage = 1860; # PeakVirtualSize = "47603712"; # PeakWorkingSetSize = 5796; # Priority = 8; # PrivatePageCount = "1544192"; # ProcessId = 1908; # QuotaNonPagedPoolUsage = 9; # QuotaPagedPoolUsage = 72; # QuotaPeakNonPagedPoolUsage = 10; # QuotaPeakPagedPoolUsage = 72; # ReadOperationCount = "0"; # ReadTransferCount = "0"; # SessionId = 0; # ThreadCount = 2; # UserModeTime = "625000"; # VirtualSize = "39182336"; # WindowsVersion = "6.3.9600"; # WorkingSetSize = "4780032"; # WriteOperationCount = "0"; # WriteTransferCount = "0"; # }; # In some circumstances - when the process is local ? - it can display the extra properties: # CommandLine = "\"C:\\Windows\\system32\\SearchFilterHost # ExecutablePath = "C:\\Windows\\system32\\SearchFilterHos lstPropNames = [ "CreationDate", "CSName", "HandleCount", "KernelModeTime", "Name", "OSName", "OtherOperationCount", "OtherTransferCount", "PageFaults", "PageFileUsage", "PeakPageFileUsage", "PeakVirtualSize", "PeakWorkingSetSize", "Priority", "PrivatePageCount", "QuotaNonPagedPoolUsage", "QuotaPagedPoolUsage", "QuotaPeakNonPagedPoolUsage", "QuotaPeakPagedPoolUsage", "ReadOperationCount", "ReadTransferCount", "SessionId", "ThreadCount", "UserModeTime", "VirtualSize", "WorkingSetSize", "WriteOperationCount", "WriteTransferCount" ] className = "CIM_Process" mapPropUnits = lib_wmi.WmiDictPropertiesUnit(cnnct, className) # There should be one process only. for wmiProc in lstProcs: grph.add((node_process, pc.property_information, lib_common.NodeLiteral(wmiProc.Description))) for prpProc in lstPropNames: valProc = getattr(wmiProc, prpProc) try: valUnit = mapPropUnits[prpProc] except KeyError: valUnit = "" valProcUnit = lib_util.AddSIUnit(valProc, valUnit) grph.add((node_process, lib_common.MakeProp(prpProc), lib_common.NodeLiteral(valProcUnit))) parent_node_process = serverBox.PidUri(wmiProc.ParentProcessId) grph.add((node_process, pc.property_ppid, parent_node_process)) cgiEnv.OutCgiRdf()
def PsutilVirtualMemorySize(proc): return lib_util.AddSIUnit(proc.memory_info().vms,"B")
def PsutilResidentSetSize(proc): return lib_util.AddSIUnit(proc.memory_info().rss,"B")
def DisplayMappedProcesses(grph, fileName): grph.add((lib_common.nodeMachine, pc.property_hostname, lib_common.NodeLiteral(lib_util.currentHostname))) # This is also a file mapped into memory. uriMappedFile = lib_common.gUriGen.FileUri(fileName) uriMemMap = None try: statinfo = os.stat(fileName) except Exception: exc = sys.exc_info()[1] grph.add((uriMappedFile, lib_common.MakeProp("Error"), lib_common.NodeLiteral(str(exc)))) return fileSize = lib_util.AddSIUnit(statinfo.st_size, "B") grph.add((uriMappedFile, pc.property_file_size, lib_common.NodeLiteral(fileSize))) propMemoryRSS = lib_common.MakeProp("Resident Set Size") for proc in CIM_Process.ProcessIter(): if lib_common.is_useless_process(proc): continue pid = proc.pid try: all_maps = CIM_Process.PsutilProcMemmaps(proc) except: # Probably psutil.AccessDenied exc = sys.exc_info()[1] # sys.stderr.write("get_memory_maps Pid=%d. Caught %s\n" % (pid,str(exc)) ) continue # sys.stderr.write("get_memory_maps OK Pid=%d:%s.\n" % (pid,str(all_maps)) ) for map in all_maps: # This, because all Windows paths are "standardized" by us. cleanMapPath = lib_util.standardized_file_path(map.path) # MapPath=C:\Windows\System32\KernelBase.dll fileName=c:\windows\system32\API-MS-WIN-CORE-LOCALIZATION-L1-1-0.DLL # sys.stderr.write("Pid=%d MapPath=%s cleanMapPath=%s fileName=%s\n" % (pid,map.path,cleanMapPath,fileName)) sameFil = lib_util.standardized_file_path( map.path) == lib_util.standardized_file_path(fileName) if sameFil: DEBUG("Pid=%d MapPath=%s cleanMapPath=%s fileName=%s", pid, map.path, cleanMapPath, fileName) # Maybe this is the first mapping we have found. if uriMemMap == None: uriMemMap = lib_common.gUriGen.MemMapUri(fileName) grph.add((uriMappedFile, pc.property_mapped, uriMemMap)) nodeProcess = lib_common.gUriGen.PidUri(pid) # The property is reversed because of display. grph.add((uriMemMap, pc.property_memmap, nodeProcess)) grph.add((nodeProcess, pc.property_pid, lib_common.NodeLiteral(pid))) # Displays the RSS only if different from the file size. if map.rss != statinfo.st_size: grph.add((nodeProcess, propMemoryRSS, lib_common.NodeLiteral(map.rss)))
def PsutilVirtualMemorySize(proc): """vms: this is the total amount of virtual memory used by the process. On UNIX it matches "top" VIRT column (see doc). On Windows this is an alias for pagefile field and it matches "Mem Usage" "VM Size" column of taskmgr.exe.""" return lib_util.AddSIUnit(proc.memory_info().vms, "B")
def PsutilResidentSetSize(proc): """rss: this is the non-swapped physical memory a process has used. On UNIX it matches "top" RES column (see doc). On Windows this is an alias for wset field and it matches "Mem Usage" column of taskmgr.exe.""" return lib_util.AddSIUnit(proc.memory_info().rss, "B")