Example #1
0
def SelectFromWhere(where_key_values):
    """This must return at least the properties defined in the ontology.
    There is no constraints on the other properties, so the query can return any set of key-value pairs,
    if the minimal set of properties is there."""

    # TODO: Add "select_attributes"
    logging.debug("CIM_Process SelectFromWhere where_key_values=%s", str(where_key_values))
    for proc_obj in psutil.process_iter():
        user_name = PsutilProcToUser(proc_obj,None)
        if user_name:
            user_name_host = lib_common.format_username(user_name)
        else:
            user_name_host = user_name

        parent_pid = proc_obj.ppid()

        if "Handle" in where_key_values and str(where_key_values["Handle"]) != str(proc_obj.pid):
            continue
        if "user" in where_key_values and where_key_values["user"] != user_name_host:
            continue
        if "parent_pid" in where_key_values and str(where_key_values["parent_pid"]) != str(parent_pid):
            continue

        # TODO: Should reuse the existing properties.
        ret_value = {
            lib_properties.MakeProp("Handle"): rdflib.Literal(proc_obj.pid),
            lib_properties.MakeProp("username"): rdflib.Literal(user_name_host),
            lib_properties.MakeProp("parent_pid"): rdflib.Literal(parent_pid)}
        yield ret_value
Example #2
0
def Main():
    cgiEnv = lib_common.CgiEnv()

    grph = cgiEnv.GetGraph()

    # [suser(name='Remi', terminal=None, host='0.246.33.0', started=1411052436.0)]

    try:
        # Windows XP, Python 3.
        try:
            # Windows XP, Python 3.4.
            users_list = psutil.users()
        except AttributeError:
            # Linux and Python 2.5
            # Windows 7, Python 3.2 : mais c'est la version de psutil qui compte.
            users_list = psutil.get_users()
    except AttributeError:
        # AttributeError: 'module' object has no attribute 'users'
        lib_common.ErrorMessageHtml("Function users() not available")

    for user in users_list:
        usrNam = lib_common.format_username(user.name)
        userNode = lib_common.gUriGen.UserUri(usrNam)

        grph.add((lib_common.nodeMachine, pc.property_user, userNode))

    cgiEnv.OutCgiRdf()
Example #3
0
def AddInfo(grph, node, entity_ids_arr):
    pid_proc = entity_ids_arr[0]
    exec_node = None
    grph.add((node, pc.property_pid, rdflib.Literal(pid_proc)))
    try:
        proc_obj = psutil.Process(int(pid_proc))

        _add_command_line_and_executable(grph, node, proc_obj)

        # A node is created with the returned string which might as well be
        # an error message, which must be unique. Otherwise all faulty nodes
        # would be merged.
        # TODO: Problem, this node is still clickable. We should return a node
        # of this same type, but with a faulty state, which would make it unclickable.
        user_name = PsutilProcToUser(proc_obj, "User access denied:PID=%s" % pid_proc)

        # TODO: Should add the hostname to the user ???
        user_name_host = lib_common.format_username(user_name)
        user_node = lib_uris.gUriGen.UserUri(user_name_host)
        grph.add((node, pc.property_user, user_node))

        sz_resid_set_sz = PsutilResidentSetSize(proc_obj)
        grph.add((node, lib_common.MakeProp("Resident Set Size"), rdflib.Literal(sz_resid_set_sz)))

        sz_virst_mem_sz = PsutilVirtualMemorySize(proc_obj)
        grph.add((node, lib_common.MakeProp("Virtual Memory Size"), rdflib.Literal(sz_virst_mem_sz)))

    except Exception as exc:
        logging.error("CIM_Process.AddInfo. Caught:%s", exc)
        grph.add((node, pc.property_information, rdflib.Literal(str(exc))))

    # Needed for other operations.
    return exec_node
Example #4
0
def AddExtraInformationtoProcess(grph, node_process, proc_obj):
    CIM_Process.AddInfo(grph, node_process, [str(proc_obj.pid)])

    usrNam = lib_common.format_username(CIM_Process.PsutilProcToUser(proc_obj))
    userNode = lib_common.gUriGen.UserUri(usrNam)
    grph.add((userNode, pc.property_owner, node_process))

    (execName, execErrMsg) = CIM_Process.PsutilProcToExe(proc_obj)
    if execName != "":
        execNod = lib_common.gUriGen.FileUri(execName)
        grph.add((node_process, pc.property_runs, execNod))
        lib_entity_file.AddInfo(grph, execNod, [execName])
Example #5
0
def AddInfo(grph,node,entity_ids_arr):
    pidProc = entity_ids_arr[0]
    exec_node = None
    grph.add( ( node, pc.property_pid, lib_common.NodeLiteral(pidProc) ) )
    try:
        proc_obj = PsutilGetProcObjNoThrow(int(pidProc))

        cmd_line = PsutilProcToCmdline(proc_obj)
        grph.add( ( node, pc.property_command, lib_common.NodeLiteral(cmd_line) ) )

        ( execName, execErrMsg ) = PsutilProcToExe(proc_obj)
        if execName == "":
            grph.add( ( node, pc.property_runs, lib_common.NodeLiteral("Executable error:"+execErrMsg) ) )
            exec_node = None
        else:
            exec_node = lib_common.gUriGen.FileUri( execName )
            grph.add( ( node, pc.property_runs, exec_node ) )

        # A node is created with the returned string which might as well be
        # an error message, which must be unique. Otherwise all faulty nodes
        # would be merged.
        # TODO: Problem, this node is still clickable. We should return a node
        # of this smae type, but with a faulty state, which would make it unclickable.
        user_name = PsutilProcToUser(proc_obj,"User access denied:PID=%s"%pidProc)

        # TODO: Should add the hostname to the user ???
        user_name_host = lib_common.format_username( user_name )
        user_node = lib_common.gUriGen.UserUri(user_name_host)
        grph.add( ( node, pc.property_user, user_node ) )

        szResidSetSz = PsutilResidentSetSize(proc_obj)
        grph.add( ( node, lib_common.MakeProp("Resident Set Size"), lib_common.NodeLiteral(szResidSetSz) ) )

        szVirstMemSz = PsutilVirtualMemorySize(proc_obj)
        grph.add( ( node, lib_common.MakeProp("Virtual Memory Size"), lib_common.NodeLiteral(szVirstMemSz) ) )

        AddLinuxCGroup(node,grph)

        # TODO: Add the current directory of the process ?

    # except psutil._error.NoSuchProcess:
    # Cannot use this exception on some psutil versions
    # AttributeError: 'ModuleWrapper' object has no attribute '_error'
    except Exception as exc:
        ERROR("CIM_Process.AddInfo. Caught:%s",exc)
        grph.add( ( node, pc.property_information, lib_common.NodeLiteral(str(exc)) ) )

    # Needed for other operations.
    return exec_node
Example #6
0
def Main():
    cgiEnv = lib_common.ScriptEnvironment()

    grph = cgiEnv.GetGraph()

    # [suser(name='John', terminal=None, host='0.246.33.0', started=1411052436.0)]
    users_list = psutil.users()

    for user in users_list:
        usr_nam = lib_common.format_username(user.name)
        user_node = lib_uris.gUriGen.UserUri(usr_nam)

        grph.add((lib_common.nodeMachine, pc.property_user, user_node))

    cgiEnv.OutCgiRdf()
Example #7
0
def Main():
    cgiEnv = lib_common.ScriptEnvironment()

    grph = cgiEnv.GetGraph()

    try:
        # Something like [suser(name='John', terminal=None, host='0.246.33.0', started=1411052436.0)]
        users_list = psutil.users()
    except Exception as exc:
        lib_common.ErrorMessageHtml("psutil.users raised:%s" % exc)

    for user in users_list:
        usr_nam = lib_common.format_username(user.name)
        user_node = lib_uris.gUriGen.UserUri(usr_nam)

        grph.add((lib_common.nodeMachine, pc.property_user, user_node))

    cgiEnv.OutCgiRdf()
Example #8
0
def SelectFromWhere( where_key_values ):
    DEBUG("CIM_Process SelectFromWhere where_key_values=%s",str(where_key_values))
    for proc_obj in ProcessIter():
        user_name = PsutilProcToUser(proc_obj,None)
        if user_name:
            user_name_host = lib_common.format_username(user_name)
        else:
            user_name_host = user_name

        parent_pid = PsutilProcToPPid(proc_obj)

        if "Handle" in where_key_values and str(where_key_values["Handle"]) != str(proc_obj.pid):
            continue
        if "user" in where_key_values and where_key_values["user"] != user_name_host:
            continue
        if "parent_pid" in where_key_values and str(where_key_values["parent_pid"]) != str(parent_pid):
            continue

        # TODO: Should reuse the existing properties.
        ret_value = {
            lib_properties.MakeProp("Handle"):lib_util.NodeLiteral(proc_obj.pid),
            lib_properties.MakeProp("username"):lib_util.NodeLiteral(user_name_host),
            lib_properties.MakeProp("parent_pid"):lib_util.NodeLiteral(parent_pid)}
        yield ret_value