Ejemplo n.º 1
0
def uadiscover():
    parser = argparse.ArgumentParser(
        description=
        "Performs OPC UA discovery and prints information on servers and endpoints."
    )
    add_minimum_args(parser)
    parser.add_argument(
        "-n",
        "--network",
        action="store_true",
        help="Also send a FindServersOnNetwork request to server")
    #parser.add_argument("-s",
    #"--servers",
    #action="store_false",
    #help="send a FindServers request to server")
    #parser.add_argument("-e",
    #"--endpoints",
    #action="store_false",
    #help="send a GetEndpoints request to server")
    args = parse_args(parser)

    client = Client(args.url, timeout=args.timeout)

    if args.network:
        print("Performing discovery at {0}\n".format(args.url))
        for i, server in enumerate(
                client.connect_and_find_servers_on_network(), start=1):
            print('Server {0}:'.format(i))
            #for (n, v) in application_to_strings(server):
            #print('  {}: {}'.format(n, v))
            print('')

    print("Performing discovery at {0}\n".format(args.url))
    for i, server in enumerate(client.connect_and_find_servers(), start=1):
        print('Server {0}:'.format(i))
        for (n, v) in application_to_strings(server):
            print('  {0}: {1}'.format(n, v))
        print('')

    for i, ep in enumerate(client.connect_and_get_server_endpoints(), start=1):
        print('Endpoint {0}:'.format(i))
        for (n, v) in endpoint_to_strings(ep):
            print('  {0}: {1}'.format(n, v))
        print('')

    sys.exit(0)
Ejemplo n.º 2
0
def uadiscover():
    parser = argparse.ArgumentParser(
        description="Performs OPC UA discovery and prints information on servers and endpoints."
    )
    add_minimum_args(parser)
    parser.add_argument(
        "-n", "--network", action="store_true", help="Also send a FindServersOnNetwork request to server"
    )
    # parser.add_argument("-s",
    # "--servers",
    # action="store_false",
    # help="send a FindServers request to server")
    # parser.add_argument("-e",
    # "--endpoints",
    # action="store_false",
    # help="send a GetEndpoints request to server")
    args = parse_args(parser)

    client = Client(args.url, timeout=args.timeout)

    if args.network:
        print("Performing discovery at {}\n".format(args.url))
        for i, server in enumerate(client.connect_and_find_servers_on_network(), start=1):
            print("Server {}:".format(i))
            # for (n, v) in application_to_strings(server):
            # print('  {}: {}'.format(n, v))
            print("")

    print("Performing discovery at {}\n".format(args.url))
    for i, server in enumerate(client.connect_and_find_servers(), start=1):
        print("Server {}:".format(i))
        for (n, v) in application_to_strings(server):
            print("  {}: {}".format(n, v))
        print("")

    for i, ep in enumerate(client.connect_and_get_server_endpoints(), start=1):
        print("Endpoint {}:".format(i))
        for (n, v) in endpoint_to_strings(ep):
            print("  {}: {}".format(n, v))
        print("")

    sys.exit(0)
Ejemplo n.º 3
0
 def test_find_servers(self):
     c = Client(URL)
     res = c.connect_and_find_servers()
     self.assertTrue(len(res) > 0)
Ejemplo n.º 4
0
 def test_find_servers(self):
     c = Client(URL)
     res = c.connect_and_find_servers()
     self.assertTrue(len(res) > 0)
Ejemplo n.º 5
0
def run():
    logger.debug("Modular Input mi_opcua command: %s" % sys.argv)
    if len(sys.argv) > 1:
        try:
            if sys.argv[1] == "--scheme":
                do_scheme()
            elif sys.argv[1] == "--validate-arguments":
                validate_arguments()
            elif sys.argv[1] == "--test":
                test()
            else:
                usage()
        except Exception as ex:
            logger.critical(ex)
    else:
        logger.debug("Modular Input mi_opcua Starts data collection.")

        configs = get_config()
        logger.debug("Configuration: %s" % configs)
        stanza = configs["name"]
        SP = configs.get("separator", ":")
        patterns = configs["measures"].split(SP)
        tout = configs["connection_timeout"].strip()
        spec = configs.get("metrics_spec", "n.a.").strip()
        timeout = 1 if len(tout) <= 0 else int(tout)

        conn = configs[
            "connection"]  ## "opc.tcp://ec2-54-190-162-94.us-west-2.compute.amazonaws.com:49320"

        if configs.has_key("username"):
            username = configs["username"].strip()
            if len(username) > 0:
                password = configs["password"].strip()
                conn = "%s?username=%s&password=%s" % (conn, username,
                                                       password)

        client = Client(conn, timeout=timeout)

        mi.init_stream(sys.stdout)
        try:
            if logger.isEnabledFor(logging.DEBUG):
                try:
                    servers = client.connect_and_find_servers()
                    logger.debug("Servers are found: ")
                    for s in servers:
                        print s
                        logger.debug("\tServer: %s" % s)
                except:
                    pass

                try:
                    nservers = client.connect_and_find_servers_on_network()
                    logger.debug("Network Servers are found: ")
                    for n in nservers:
                        logger.debug("\tNetwork Server: %s" % n)
                except:
                    pass

                endpoints = None
                try:
                    endpoints = client.connect_and_get_server_endpoints()
                    logger.debug("Server Endpoints are found: ")
                    for e in endpoints:
                        logger.debug("\tServer Endpoint: %s" % e.EndpointUrl)
                        logger.debug("\t\tServer Details: %s" % e)
                except:
                    pass

            try:
                logger.info("Start connecting OPC Server [%s]." % conn)
                client.connect()
                logger.info("OPC Server [%s] is connected." % conn)
            except Exception as ex:
                logger.error("Connecting to [%s] failed." % conn)
                if endpoints and len(endpoints) > 0:
                    for ep in endpoints:
                        try:
                            conn = ep.EndpointUrl
                            logger.info(
                                "Try connect to another OPC Server [%s]." %
                                conn)
                            client = Client(conn, timeout=timeout)
                            client.connect()
                            logger.info("OPC Server [%s] is connected." % conn)
                        except:
                            break
                else:
                    raise ex

            measures = []
            root = client.get_root_node()

            node.collect_measures(measures, patterns, root)

            md = {}
            try:
                jm = os.path.join(ua.data_dir(), spec)
                with open(jm, 'r') as mfp:
                    md = json.load(mfp)
                    mfp.close()
            except:
                pass

            for m in measures:
                collect_data(stanza, m[len(m) - 1], spec=md)

        except Exception as ex:
            logger.critical(ex)
        finally:
            mi.fini_stream(sys.stdout)
            logger.info("---- end of opc ua ----")
            client.disconnect()