Пример #1
0
def main():
    conf = AppConfig([
        ("server_address", str, "Address of SYSmon server."),
        ("server_verify_ssl", bool, "Verify SYSmon server's SSL certificate?")
    ], [
        AppConfig.argument("stamp", type=str, nargs=1, help="Stamp name"),
        AppConfig.argument("command", metavar="COMMAND", type=str, nargs=argparse.REMAINDER, help="Optional command to execute.")
    ])

    exitcode = 0

    if len(conf.command):
        logging.debug("Executing command %s" % (conf.command))
        try:
            exitcode = subprocess.call(conf.command, shell=len(conf.command) == 1)
        except Exception as e:
            logging.exception(e)
            exitcode = 255

    if exitcode == 0:
        logging.info("Writing stamp `%s`." % (conf.stamp[0], ))
        hostname = socket.gethostname()
        requests.get("%sstamps/put/%s/%s" % (conf.server_address, hostname, conf.stamp[0]), verify=conf.get("server_verify_ssl", True))
        sys.exit(0)
    else:
        logging.info("NOT writing stamp `%s` because of bad exit code %d." % (conf.stamp[0], exitcode))
        sys.exit(exitcode)
Пример #2
0
def main():
    conf = AppConfig([("server_address", str, "Address of SYSmon server."),
                      ("server_verify_ssl", bool,
                       "Verify SYSmon server's SSL certificate?")])

    log = logging.getLogger()
    log.info("SYSmon SNMP agent is starting.")

    try:
        server_address = conf.get("server_address")
        if server_address is None:
            raise Exception("No server address configured.")

        hostname = socket.gethostname()
        url = "%shosts/list/%s" % (server_address, hostname)
        r = requests.get(url, verify=conf.get("server_verify_ssl", True))
        devices = json.loads(r.text)

        for device in devices:
            try_device(conf, log, device)

    except Exception as e:
        log.error(str(e))
        log.debug(traceback.format_exc())

    log.info("SYSmon SNMP agent finished.")
Пример #3
0
def main():
    conf = AppConfig([("server_address", str, "Address of SYSmon server."),
                      ("server_verify_ssl", bool,
                       "Verify SYSmon server's SSL certificate?")])

    log = logging.getLogger()
    log.info("SYSmon collector is starting.")

    try:
        server_address = conf.get("server_address")
        if server_address is None:
            raise Exception("No server address configured.")

        hostname = get_hostname()
        distribution, version = get_system_version()
        kernel = get_system_kernel()
        uptime = get_uptime()

        packages = get_packages()

        r = requests.put("%s/collect.php" % (conf.get("server_address"), ),
                         data=json.dumps({
                             "hostname": hostname,
                             "distribution": distribution,
                             "version": version,
                             "kernel": kernel,
                             "uptime": uptime,
                             "packages": packages
                         }),
                         verify=conf.get("server_verify_ssl", True))

        if r.status_code != 200:
            log.error(r.text)
        else:
            log.debug(r.text)
    except Exception as e:
        log.error(str(e))
        log.debug(traceback.format_exc())

    log.info("SYSmon collector finished.")
Пример #4
0
def main():
    conf = AppConfig([
        ("server_address", str, "Address of SYSmon server."),
        ("server_verify_ssl", bool, "Verify SYSmon server's SSL certificate?"),
        ("dir", str,
         "Directory to search for checks. To specify more than one path, separate paths with semicolon."
         )
    ])

    log = logging.getLogger()
    log.info("SYSmon checker is starting.")

    try:
        server_address = conf.get("server_address")
        if server_address is None:
            raise Exception("No server address configured.")

        hostname = socket.gethostname()
        url = "%s/checks/list/%s" % (server_address, hostname)
        checks = json.loads(
            requests.get(url, verify=conf.get("server_verify_ssl", True)).text)

        response = []

        known_check = find_checks(
            conf.get("dir", "/usr/share/gcm-sysmon/checks/").split(";"))

        for check in checks:
            check_name = check["type"]
            try:
                response.append(known_check[check_name].execute(check))
            except KeyError as e:
                response.append({
                    "id":
                    check["id"],
                    "alerts": [{
                        "type": "check_unavailable",
                        "data": {
                            "type": check_name
                        }
                    }]
                })

        if response:
            r = requests.put("%s/checks/put" % (server_address, ),
                             data=json.dumps(response),
                             verify=conf.get("server_verify_ssl", True))

            if r.status_code != 200:
                logging.error(r.text)
    except Exception as e:
        log.error(str(e))
        log.debug(traceback.format_exc())

    log.info("SYSmon checker finished.")
Пример #5
0
 def test_conv_bool_t(self):
     self.assertTrue(AppConfig._get_bool(True))
Пример #6
0
 def test_fail_gibrish(self):
     with self.assertRaises(ValueError):
         self.assertFalse(AppConfig._get_bool("gibrish"))
Пример #7
0
 def test_conv_u_name_f(self):
     self.assertFalse(AppConfig._get_bool(u"False"))
     self.assertFalse(AppConfig._get_bool(u"FALSE"))
     self.assertFalse(AppConfig._get_bool(u"false"))
Пример #8
0
 def test_conv_u_name_t(self):
     self.assertTrue(AppConfig._get_bool(u"True"))
     self.assertTrue(AppConfig._get_bool(u"TRUE"))
     self.assertTrue(AppConfig._get_bool(u"true"))
Пример #9
0
 def test_conv_u_num_f(self):
     self.assertFalse(AppConfig._get_bool(u"0"))
Пример #10
0
 def test_conv_u_num_t(self):
     self.assertTrue(AppConfig._get_bool(u"1"))
Пример #11
0
 def test_conv_int_f(self):
     self.assertFalse(AppConfig._get_bool(0))
Пример #12
0
 def test_conv_int_t(self):
     self.assertTrue(AppConfig._get_bool(1))
Пример #13
0
 def test_conv_bool_f(self):
     self.assertFalse(AppConfig._get_bool(False))