Exemplo n.º 1
0
def Main():

    cgiEnv = lib_common.ScriptEnvironment( )

    instance_name = cgiEnv.m_entity_id_dict["Instance"]
    session_id = cgiEnv.m_entity_id_dict["Id"]

    instance_node = survol_mysql_instance.MakeUri(instance_name)

    hostname, hostport = survol_mysql.InstanceToHostPort(instance_name)

    cgiEnv = lib_common.ScriptEnvironment()

    grph = cgiEnv.GetGraph()

    # BEWARE: The rule whether we use the host name or the host IP is not very clear !
    # The IP address would be unambiguous but less clear.
    host_node = lib_uris.gUriGen.HostnameUri(hostname)

    # BEWARE: This is duplicated.
    propDb = lib_common.MakeProp("Mysql database")

    a_cred = lib_credentials.GetCredentials("MySql", instance_name)

    conn_mysql = survol_mysql.MysqlConnect(instance_name, aUser=a_cred[0], aPass=a_cred[1])

    cursor_mysql = conn_mysql.cursor()

    # mysql> select * from information_schema.processlist;
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+
    # | ID     | USER             | HOST             | DB   | COMMAND | TIME | STATE     | INFO                                         |
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+
    # | 439768 | primhilltcsrvdb1 | 10.2.123.9:52146 | NULL | Query   |    0 | executing | select * from information_schema.processlist |
    # | 439765 | primhilltcsrvdb1 | 10.2.123.9:52062 | NULL | Sleep   |   13 |           | NULL                                         |
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+

    cursor_mysql.execute("select * from information_schema.processlist where ID=%s" % session_id)

    grph.add((host_node, lib_common.MakeProp("Mysql instance"), instance_node))

    # There should be one row only.
    for sess_info in cursor_mysql:
        logging.debug("sess_info=%s", str(sess_info))

        mysql_session_id = sess_info[0]
        mysql_user = sess_info[1]

        session_node = survol_mysql_session.MakeUri(instance_name, mysql_session_id)

        # If there is a proper socket, then create a name for it.
        mysql_socket = sess_info[2]
        try:
            mysql_socket_host, mysql_socket_port = mysql_socket.split(":")
            socket_node = lib_uris.gUriGen.AddrUri(mysql_socket_host, mysql_socket_port)
            grph.add((session_node, lib_common.MakeProp("Connection socket"), socket_node))
        except:
            pass

        mysql_db = sess_info[3]
        grph.add((session_node, lib_common.MakeProp("Database"), lib_util.NodeLiteral(mysql_db)))

        mysql_time = sess_info[5]
        grph.add((session_node, lib_common.MakeProp("Time"), lib_util.NodeLiteral(mysql_time)))

        # If there is a running query, then display it.
        mysql_command = sess_info[4]
        mysql_state = sess_info[6]
        if (mysql_command == "Query") and (mysql_state == "executing"):
            mysql_query = sess_info[7]

            node_query = survol_mysql_query.MakeUri(instance_name,mysql_query)
            grph.add((session_node, lib_common.MakeProp("Mysql query"), node_query))

        grph.add((session_node, lib_common.MakeProp("Command"), lib_util.NodeLiteral(mysql_command)))

        grph.add((session_node, lib_common.MakeProp("State"), lib_util.NodeLiteral(mysql_state)))

        grph.add((session_node, lib_common.MakeProp("User"), lib_util.NodeLiteral(mysql_user)))

        grph.add((session_node, lib_common.MakeProp("Mysql session"), instance_node))

    cursor_mysql.close()
    conn_mysql.close()

    cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
Exemplo n.º 2
0
def Main():

    cgiEnv = lib_common.CgiEnv()

    instanceName = cgiEnv.m_entity_id_dict["Instance"]
    instanceNode = survol_mysql_instance.MakeUri(instanceName)

    (hostname, hostport) = survol_mysql.InstanceToHostPort(instanceName)

    cgiEnv = lib_common.CgiEnv()

    grph = cgiEnv.GetGraph()

    hostAddr = lib_util.GlobalGetHostByName(hostname)

    # BEWARE: The rule whether we use the host name or the host IP is not very clear !
    # The IP address would be unambiguous but less clear.
    hostNode = lib_common.gUriGen.HostnameUri(hostname)

    # BEWARE: This is duplicated.
    propDb = lib_common.MakeProp("Mysql database")

    #nodeMysqlDatabase = survol_mysql_database.MakeUri(instanceName,dbNam)
    #grph.add( ( hostNode, propDb, nodeMysqlDatabase ) )

    aCred = lib_credentials.GetCredentials("MySql", instanceName)

    connMysql = survol_mysql.MysqlConnect(instanceName,
                                          aUser=aCred[0],
                                          aPass=aCred[1])

    cursorMysql = connMysql.cursor()

    # mysql> select * from information_schema.processlist;
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+
    # | ID     | USER             | HOST             | DB   | COMMAND | TIME | STATE     | INFO                                         |
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+
    # | 439768 | primhilltcsrvdb1 | 10.2.123.9:52146 | NULL | Query   |    0 | executing | select * from information_schema.processlist |
    # | 439765 | primhilltcsrvdb1 | 10.2.123.9:52062 | NULL | Sleep   |   13 |           | NULL                                         |
    # +--------+------------------+------------------+------+---------+------+-----------+----------------------------------------------+

    cursorMysql.execute("select * from information_schema.processlist")

    propTable = lib_common.MakeProp("Mysql table")

    grph.add((hostNode, lib_common.MakeProp("Mysql instance"), instanceNode))

    for sessInfo in cursorMysql:
        sys.stderr.write("sessInfo=%s\n" % str(sessInfo))

        mysqlSessionId = sessInfo[0]
        mysqlUser = sessInfo[1]

        sessionNode = survol_mysql_session.MakeUri(instanceName,
                                                   mysqlSessionId)

        # If there is a proper socket, then create a name for it.
        mysqlSocket = sessInfo[2]
        try:
            (mysqlSocketHost, mysqlSocketPort) = mysqlSocket.split(":")
            socketNode = lib_common.gUriGen.AddrUri(mysqlSocketHost,
                                                    mysqlSocketPort)
            grph.add((sessionNode, lib_common.MakeProp("Connection socket"),
                      socketNode))
        except:
            pass

        # If there is a running query, then display it.
        mysqlCommand = sessInfo[4]
        mysqlState = sessInfo[6]
        if (mysqlCommand == "Query") and (mysqlState == "executing"):
            mysqlQuery = sessInfo[7]

            nodeQuery = survol_mysql_query.MakeUri(instanceName, mysqlQuery)
            grph.add(
                (sessionNode, lib_common.MakeProp("Mysql query"), nodeQuery))

        grph.add((sessionNode, lib_common.MakeProp("User"),
                  lib_common.NodeLiteral(mysqlUser)))

        grph.add(
            (sessionNode, lib_common.MakeProp("Mysql session"), instanceNode))

    cursorMysql.close()
    connMysql.close()

    cgiEnv.OutCgiRdf("LAYOUT_SPLINE")