Ejemplo n.º 1
0
    def get_rrd_data(self,hostname):
        try:

            rrd_url = "http://"+self.xs_username+":"+self.xs_password+"@"+hostname
            rrd_parameters = {}
            rrd_data = parse_rrd.RRDUpdates()
            rrd_data.refresh(rrd_url,rrd_parameters)

        except Exception, e:
            raise
Ejemplo n.º 2
0
def check_cpu(session, args):

    warning = args["warning"]
    critical = args["critical"]

    import parse_rrd
    params = {}
    hosts = session.xenapi.host.get_all_records()

    params['cf'] = "AVERAGE"
    params['start'] = int(time.time()) - 300
    params['interval'] = 5
    params['host'] = "true"

    perfdata = {}
    for host in hosts:
        v= []
        url = scheme+session.xenapi.host.get_address(host)
        rrd_updates = parse_rrd.RRDUpdates()
        rrd_updates.refresh(session.handle, params, url)
        paramList = ['cpu'+session.xenapi.host_cpu.get_record(i)['number'] for i in session.xenapi.host_cpu.get_all_records() if host in session.xenapi.host_cpu.get_record(i)['host'] ]
        latest_row = get_latest_row(rrd_updates)
        for param in rrd_updates.get_host_param_list():
            if param in paramList:
                v.append(float(rrd_updates.get_host_data(param, latest_row)))
        perfdata[session.xenapi.host.get_name_label(host)] = reduce(lambda x, y: x+y, v)/len(v)

    exitcode = 0
    globalperf = 0
    for perf in perfdata:
        globalperf += perfdata[perf]
        if perfdata[perf] > float(critical)/100:
            exitcode = 2
            prefix = "CRITICAL: CPU "
        elif perfdata[perf] > float(warning)/100:
            exitcode = 1
            prefix = "WARNING: CPU "
        else:
            exitcode = 0
            prefix = "OK: CPU "

    globalperf = globalperf / len(perfdata)
    print prefix + "| 'used_cpu'="+str(round(globalperf, 2)*100)+"%;" + str(warning)+"%;" + str(critical)+"%;0%;100%;\n"+\
    ";\n".join([host+" Used CPU = "+str(round(perfdata[host],2)*100) for host in perfdata]) + "%; |" +\
    " ".join(["'"+host+"_used_cpu'="+str(round(perfdata[host],2)*100)+"%;"+str(warning)+"%;" + str(critical)+"%;0%;100%" for host in perfdata])

    sys.exit(exitcode)
Ejemplo n.º 3
0
    def __init__(self):
        self.params = {}
        self.url = "http://localhost"
        self.x = XenAPI.xapi_local()

        try:
            self.x.login_with_password("root", "password")
            self.xapi = self.x.xenapi
            self.rrd_updates = parse_rrd.RRDUpdates()
            self.params['cf'] = "AVERAGE"
            self.params['start'] = int(time.time()) - 10
            self.params['interval'] = 5
            self.params['host'] = "true"

            self.rrd_updates.refresh(self.x.handle, self.params, self.url)
        finally:
            self.x.logout()
Ejemplo n.º 4
0
def main():
    url = "http://10.108.8.93"
    session = XenAPI.Session(url)
    session.xenapi.login_with_password('root', 'VVTLiuztTP3F')

    rrd_updates = parse_rrd.RRDUpdates()
    params = {}
    params['cf'] = "AVERAGE"
    params['start'] = int(time.time()) - 10
    params['interval'] = 5
    params['host'] = "true"
    rrd_updates.refresh(session.handle, params, url)

    if params['host'] == 'true':
        print_latest_host_data(rrd_updates)

    for uuid in rrd_updates.get_vm_list():
        print_latest_vm_data(rrd_updates, uuid)
        param = 'cpu0'
        data = build_vm_graph_data(rrd_updates, uuid, param)
        fh = open("%s-%s.dat" % (uuid, param), 'w')
        fh.write(data)
        fh.close()
Ejemplo n.º 5
0
def check_cpu(session, args):

    warning = args["warning"]
    critical = args["critical"]

    import parse_rrd
    params = {}
    hosts = session.xenapi.host.get_all_records()

    params['cf'] = "AVERAGE"
    params['start'] = int(time.time()) - 300
    params['interval'] = 5
    params['host'] = "true"

    perfdata = {}
    for host in hosts:
        v = []
        url = scheme + session.xenapi.host.get_address(host)
        rrd_updates = parse_rrd.RRDUpdates()
        rrd_updates.refresh(session.handle, params, url)
        paramList = [
            'cpu' + session.xenapi.host_cpu.get_record(i)['number']
            for i in session.xenapi.host_cpu.get_all_records()
            if host in session.xenapi.host_cpu.get_record(i)['host']
        ]
        for param in rrd_updates.get_host_param_list():
            if param in paramList:
                max_time = 0
                data = ""
                for row in range(rrd_updates.get_nrows()):
                    epoch = rrd_updates.get_row_time(row)
                    dv = str(rrd_updates.get_host_data(param, row))
                    if epoch > max_time:
                        max_time = epoch
                        data = dv
                if data == "":
                    data = 0
                v.append(float(data))
        perfdata[session.xenapi.host.get_name_label(host)] = reduce(
            lambda x, y: x + y, v) / len(v)

    exitcode = 0
    globalperf = 0
    for perf in perfdata:
        globalperf += perfdata[perf]
        if perfdata[perf] > float(critical) / 100:
            exitcode = 2
            prefix = "CRITICAL - CPU: "
        elif perfdata[perf] > float(warning) / 100:
            exitcode = 1
            prefix = "WARNING - CPU: "
        else:
            exitcode = 0
            prefix = "OK - CPU: "

    globalperf = globalperf / len(perfdata)
    #    print prefix + "| used_cpu="+str(round(globalperf, 2)*100)+"%;" + str(warning)+"%;" + str(critical)+"%\n"+\
    #    "\n".join([host+" Used CPU = "+str(round(perfdata[host],2)*100)+"%" for host in perfdata]) + " | " +\
    #    "\n".join([host+"_used_cpu="+str(round(perfdata[host],2)*100)+"%;"+str(warning)+"%;" + str(critical)+"%" for host in perfdata])
    print prefix + ", ".join([host+" CPU = "+str(round(perfdata[host],2)*100)+"%" for host in perfdata]) + " | " + \
    "average_used_cpu=" + str(round(globalperf, 2)*100) + "%;" + str(warning) + "%;" + str(critical) + "% " + \
    " ".join([host+"_used_cpu="+str(round(perfdata[host],2)*100)+"%;"+str(warning)+"%;" + str(critical)+"%" for host in perfdata])

    sys.exit(exitcode)
Ejemplo n.º 6
0
    def run(self):
        sys.stderr.write('xen2monitis \n')
        sys.stderr.flush()
        # gets last time
        try:
            time_file
        except NameError:
            time_file = ('/usr/local/lib/xen2monitis/time.file')

        last_time = get_last_time(time_file)

        try:
            secret_file
        except NameError:
            secret_file = ('/usr/local/lib/xen2monitis/secret')

        while True:
            # reads secretes from given file
            try:
                secrets = open(secret_file).read().rstrip('/n').split(',')
                secrets = [v.strip() for v in secrets]
            except Exception:
                secrets = []
            try:
                key = secrets[0]
                secret = secrets[1]
            except IndexError:
                sys.stderr.write(
                    "Information necessary to connect to Monitis API not define in secret file\n"
                )
                self.delpid()
                sys.exit(2)
            try:
                password = secrets[2]
            except IndexError:
                sys.stderr.write(
                    "Password necessary to connect to XenServer not define in secret file\n"
                )
                self.delpid()
                sys.exit(2)

            # establishes connection to Monitis
            mon_obj = monitis_connection(key, secret)

            # creates new object for data updates
            rrd_updates = parse_rrd.RRDUpdates()

            # dictionary (as in original example) to update default value
            params = {}
            params['cf'] = "AVERAGE"
            params['interval'] = 60
            params['host'] = 'true'

            params['start'] = last_time

            try:
                xs_url
            except NameError:
                xs_url = 'http://localhost'

            # establish connection to XenServer
            xs_conn, xs_token = xs_connection(password, xs_url)

            # update data
            rrd_updates.refresh(xs_token, params, xs_url)

            # LOOP TO ADD RESULTS FOR HOSTS
            if params['host'] == 'true':
                # LOOP OVER
                for host_ref in xs_conn.host.get_all(xs_token)['Value']:
                    # LOOP OVER ALL HOSTS
                    for param_name in rrd_updates.get_host_param_list():
                        vm_uuid = rrd_updates.get_host_uuid()
                        monitor_name = "%s - %s (HOST: %s)" % (
                            xs_conn.host.get_hostname(
                                xs_token,
                                host_ref)['Value'], param_name, vm_uuid)
                        sys.stderr.write(monitor_name + '\n')
                        if monitor_name not in mon_obj.dictMonitors().keys():
                            mon_obj.addMonitor(
                                name=monitor_name,
                                resultParams='1m: 1 Min. Average: :4',
                                tag='XenServer')
                        for row in range(rrd_updates.get_nrows()):
                            data = str(
                                rrd_updates.get_host_data(param_name, row))
                            data_time = str(
                                rrd_updates.get_row_time(row) * 1000)
                            monitor_id = mon_obj.dictMonitors(
                            )[monitor_name][0]
                            mon_obj.addResult(monitorId=monitor_id,
                                              result='1m:' + data,
                                              checkTime=data_time)

            # LOOP TO ADD RESULTS FOR VM
            # LOOP OVER ALL VMs
            for vm_uuid in rrd_updates.get_vm_list():
                # LOOP OVER ALL PARAMS FOR GIVEN VM
                for param_name in rrd_updates.get_vm_param_list(vm_uuid):
                    # check if monitor exist
                    vm_ref = xs_conn.VM.get_by_uuid(xs_token, vm_uuid)['Value']
                    monitor_name = "%s - %s (VM: %s)" % (
                        xs_conn.VM.get_name_label(
                            xs_token, vm_ref)['Value'], param_name, vm_uuid)
                    sys.stderr.write(monitor_name + '\n')
                    if monitor_name not in mon_obj.dictMonitors().keys():
                        mon_obj.addMonitor(
                            name=monitor_name,
                            resultParams='1m: 1 Min. Average: :4',
                            tag='XenServer')
                        # ALL RESULT NEWER THAN LAST CHECK
                    for row in range(rrd_updates.get_nrows()):
                        data = str(
                            rrd_updates.get_vm_data(vm_uuid, param_name, row))
                        data_time = str(rrd_updates.get_row_time(row) * 1000)
                        monitor_id = mon_obj.dictMonitors()[monitor_name][0]
                        mon_obj.addResult(
                            monitorId=monitor_id,
                            result='1m:' + data,
                            checkTime=data_time
                        )  #what is check time - I think it what I expect it is.

            # update time of operation
            try:
                open(time_file, 'w').write("%d" % time.time())
            except IOError as e:
                sys.stderr.write("PROBLEM WITH TIME FILE\n %s" % e)