def _vmstat_to_graph(cgiEnv, vmstat_header, input_line): grph = cgiEnv.ReinitGraph() split_header = vmstat_header.split() split_line = input_line.split() if len(split_header) != len(split_line): logging.error("Different lengths: [%s] / [%s]" % (split_header, split_line)) return current_node_hostname = lib_uris.gUriGen.HostnameUri( lib_util.currentHostname) property_vmstat = lib_properties.MakeProp("vmstat") sample_root_node = rdflib.BNode() grph.add((current_node_hostname, property_vmstat, sample_root_node)) timestamp_node = lib_kbase.time_stamp_now_node() grph.add((sample_root_node, pc.property_information, timestamp_node)) for column_name, column_value in zip(split_header, split_line): if column_name == "": continue # Column name is binary and converted to unicode. property_node = lib_properties.MakeProp("vmstat.%s" % column_name.decode()) # TODO: Add a timestamp. grph.add((sample_root_node, property_node, lib_util.NodeLiteral(column_value))) cgiEnv.OutCgiRdf("LAYOUT_RECT", [property_vmstat])
def Snapshot(): cgiEnv = lib_common.ScriptEnvironment() grph = cgiEnv.GetGraph() sample_root_node = rdflib.BNode() logging.debug("Adding net_io_counters") try: _add_system_counters_to_sample_node(grph, sample_root_node) except Exception as exc: logging.error("Caught:%s" % exc) logging.debug("Added net_io_counters") property_system_counters = lib_properties.MakeProp("system_counters") current_node_hostname = lib_uris.gUriGen.HostnameUri( lib_util.currentHostname) # TODO: pc.property_information is the default property for sorting. # TODO: This could use a specific timestamp property, for example "point in time" P585 timestamp_node = lib_kbase.time_stamp_now_node() grph.add((sample_root_node, pc.property_information, timestamp_node)) grph.add( (current_node_hostname, property_system_counters, sample_root_node)) cgiEnv.OutCgiRdf("LAYOUT_RECT", [property_system_counters])
def send_events_once(): cgiEnv = lib_common.ScriptEnvironment() path_to_watch = cgiEnv.GetId() logging.info("path_to_watch=%s" % path_to_watch) h_dir = win32file.CreateFile( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None) while True: # Not too fast. time.sleep(5) grph = cgiEnv.ReinitGraph() # # ReadDirectoryChangesW takes a previously-created handle to a directory, a buffer size for results, # a flag to indicate whether to watch subtrees and a filter of what changes to notify. # # NB Tim Juchcinski reports that he needed to up the buffer size to be sure of picking up all # events when a large number of files were deleted at once. # # TODO: Use asynchronous completion ? # https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw # https://stackoverflow.com/questions/49799109/win32file-readdirectorychangesw-doesnt-find-all-moved-files results = win32file.ReadDirectoryChangesW( h_dir, 65536, True, win32con.FILE_NOTIFY_CHANGE_FILE_NAME | win32con.FILE_NOTIFY_CHANGE_DIR_NAME | win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | win32con.FILE_NOTIFY_CHANGE_SIZE | win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | win32con.FILE_NOTIFY_CHANGE_SECURITY, None, None) timestamp_node = lib_kbase.time_stamp_now_node() for action_code, updated_file in results: _add_windows_dir_change(grph, path_to_watch, updated_file, action_code, timestamp_node) cgiEnv.OutCgiRdf("LAYOUT_RECT", [property_notified_file_change])
def Snapshot(): global _global_counter cgiEnv = lib_common.ScriptEnvironment(parameters={ _param_a: _global_counter, _param_b: "Two" }) _global_counter += 1 # This is to ensure that all CGI parameters are handled. parameter_a = int(cgiEnv.get_parameters(_param_a)) parameter_b = cgiEnv.get_parameters(_param_b) grph = cgiEnv.ReinitGraph() current_pid = os.getpid() node_process = lib_uris.gUriGen.PidUri(current_pid) param_a_property = lib_properties.MakeProp(_param_a) param_b_property = lib_properties.MakeProp(_param_b) sample_root_node = rdflib.BNode() # TODO: pc.property_information is the default property for sorting by time-stamp. # TODO: This could use a specific timestamp property, for example "point in time" P585 timestamp_node = lib_kbase.time_stamp_now_node() grph.add((sample_root_node, pc.property_information, timestamp_node)) grph.add((sample_root_node, param_a_property, lib_util.NodeLiteral(parameter_a))) grph.add((sample_root_node, param_b_property, lib_util.NodeLiteral(parameter_b))) property_sample = lib_properties.MakeProp("sample") grph.add((node_process, property_sample, sample_root_node)) cgiEnv.OutCgiRdf("LAYOUT_RECT", [property_sample])
def Snapshot(): cgiEnv = lib_common.ScriptEnvironment() grph = cgiEnv.GetGraph() cpu_property = lib_properties.MakeProp("cpu") rss_property = lib_properties.MakeProp("rss") vms_property = lib_properties.MakeProp("vms") property_process_perf = lib_properties.MakeProp("Processes performances") for proc in psutil.process_iter(): node_process = lib_uris.gUriGen.PidUri(proc.pid) sample_root_node = rdflib.BNode() grph.add((node_process, property_process_perf, sample_root_node)) timestamp_node = lib_kbase.time_stamp_now_node() # TODO: pc.property_information is the default property for sorting. # TODO: This could use a specific timestamp property, for example "point in time" P585 grph.add((sample_root_node, pc.property_information, timestamp_node)) cpu_percent = proc.cpu_percent(interval=0) grph.add((sample_root_node, cpu_property, lib_util.NodeLiteral(cpu_percent))) try: memory_dict = proc.memory_full_info() grph.add((sample_root_node, rss_property, lib_util.NodeLiteral(memory_dict.rss))) grph.add((sample_root_node, vms_property, lib_util.NodeLiteral(memory_dict.vms))) except psutil.AccessDenied: pass cgiEnv.OutCgiRdf("LAYOUT_RECT", [property_process_perf])