Example #1
0
def main():
    (options, args) = parse_args()

    if options.host == 'localhost':
        hostname = socket.getfqdn()
    else:
        hostname = options.host

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(hostname)

    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(r, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "varnish.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
Example #2
0
def long_run(data_type, debug_level):
    """
    Generic long running process simulator
    Used by tests below
    """
    zbx_container = protobix.DataContainer()
    zbx_container.debug_level = debug_level
    run = 1
    max_run = 1000
    while run <= max_run:
        zbx_container.data_type = data_type
        zbx_container.add(PAYLOAD[data_type])
        try:
            zbx_container.send()
        except:
            pass
        if run % (max_run / 10) == 0 or run <= 1:
            usage = resource.getrusage(resource.RUSAGE_SELF)
            display_memory = usage[2] * resource.getpagesize() / 1000000.0
            if run == 1:
                initial_memory = usage[2]
                display_initial_memory = usage[2] * resource.getpagesize(
                ) / 1000000.0
            final_memory = usage[2]
            print('Run %i: ru_maxrss=%f mb - initial=%f mb' %
                  (run, (display_memory), display_initial_memory))
        run += 1
    return initial_memory, final_memory
Example #3
0
    def __init__(self, config_path="/etc/mqtt-zabbix/mqtt-zabbix.cfg"):
        # Read the config file
        config = configparser.RawConfigParser()
        config.read(config_path)

        # Use ConfigParser to pick out the settings
        self.DEBUG = config.getboolean("global", "DEBUG")
        self.LOGFILE = config.get("global", "LOGFILE")
        self.MQTT_HOST = config.get("global", "MQTT_HOST")
        self.MQTT_PORT = config.getint("global", "MQTT_PORT")
        self.MQTT_TOPIC = config.get("global", "MQTT_TOPIC")
        self.KEYHOST = config.get("global", "KEYHOST")
        self.KEYFILE = config.get("global", "KEYFILE")

        self.APPNAME = "mqtt-zabbix"
        self.PRESENCETOPIC = "clients/" + socket.getfqdn() + "/" + self.APPNAME + "/state"
        client_id = self.APPNAME + "_%d" % os.getpid()
        self.mqttc = mqtt.Client(client_id=client_id)

        LOGFORMAT = "%(asctime)-15s %(message)s"

        if self.DEBUG:
            logging.basicConfig(filename=self.LOGFILE,
                                level=logging.DEBUG,
                                format=LOGFORMAT)
        else:
            logging.basicConfig(filename=self.LOGFILE,
                                level=logging.INFO,
                                format=LOGFORMAT)

        self.zbx_sender = protobix.DataContainer()
Example #4
0
 def test_custom_params(self):
     self.zbx_container = protobix.DataContainer(
         data_type='items',
         zbx_file='./tests/zabbix_agentd.conf',
         zbx_host='localhost',
         zbx_port=10052,
         log_level=4,
         log_output='/var/log/zabbix_agentd.log',
         dryrun=True)
     assert self.zbx_container.zbx_host == \
            self.zbx_container._config['server'] == 'localhost'
     assert self.zbx_container.zbx_port == \
            self.zbx_container._config['port'] == 10052
     assert self.zbx_container.data_type == \
            self.zbx_container._config['data_type'] == 'items'
     assert self.zbx_container.log_level == \
            self.zbx_container._config['log_level'] == 4
     assert self.zbx_container.dryrun == \
            self.zbx_container._config['dryrun'] == True
     assert self.zbx_container.items_list == []
     assert self.zbx_container._config == {
         'server': 'localhost',
         'port': 10052,
         'log_level': 4,
         'log_output': '/var/log/zabbix_agentd.log',
         'dryrun': True,
         'data_type': 'items',
         'timeout': 3
     }
def test_invalid_logger():
    """
    Adding data before assigning data_type should raise an Exception
    """
    with pytest.raises(ValueError) as err:
        zbx_datacontainer = protobix.DataContainer(logger='invalid')
    assert str(err.value) == 'logger requires a logging instance'
Example #6
0
def test_handle_response_with_invalid_content(zabbix_answer):
    """
    Test Zabbix Server/Proxy answer
    """
    payload = '{"response":"success","info":"' + zabbix_answer + '"}'
    zbx_datacontainer = protobix.DataContainer()
    with pytest.raises(IndexError):
        zbx_datacontainer._handle_response(payload)
Example #7
0
        def zabbix_send(value):
            cfg = protobix.ZabbixAgentConfig()
            cfg.server_active = trapper
            zbx = protobix.DataContainer(cfg)

            zbx.data_type = 'items'
            zbx.add_item(host='Zabbix server', key='test.alerta', value=value)
            response = zbx.send()
            print(response)
def test_items_add_before_set_data_type(data_type):
    """
    Adding data before assigning data_type should raise an Exception
    """
    zbx_datacontainer = protobix.DataContainer()
    assert zbx_datacontainer.items_list == []
    with pytest.raises(ValueError):
        zbx_datacontainer.add(DATA[data_type])
    assert zbx_datacontainer.items_list == []
    zbx_datacontainer.data_type = data_type
    zbx_datacontainer.add(DATA)
    assert len(zbx_datacontainer.items_list) == 4
 def setUp(self):
     self.zbx_container = protobix.DataContainer()
     self.zbx_container._items_list = []
     self.zbx_container._config = {
         'server': '127.0.0.1',
         'port': 10051,
         'log_level': 3,
         'log_output': '/tmp/zabbix_agentd.log',
         'dryrun': False,
         'data_type': None,
         'timeout': 3,
     }
Example #10
0
def test_handle_response(zabbix_answer):
    """
    Test Zabbix Server/Proxy answer
    """
    payload = '{"response":"success","info":"' + zabbix_answer + '"}'
    zbx_datacontainer = protobix.DataContainer()
    srv_response, processed, failed, total, time = zbx_datacontainer._handle_response(
        payload)
    assert srv_response == 'success'
    assert processed == 1
    assert failed == 2
    assert total == 3
    assert time == 0.123456
Example #11
0
def test_server_connection_fails(data_type):
    """
    Connection to Zabbix Server fails
    """
    zbx_datacontainer = protobix.DataContainer()
    zbx_datacontainer.server_port = 10060
    zbx_datacontainer.data_type = data_type
    assert zbx_datacontainer.items_list == []
    assert zbx_datacontainer.server_port == 10060
    zbx_datacontainer.add(DATA[data_type])
    with pytest.raises(IOError):
        zbx_datacontainer.send()
    assert zbx_datacontainer.items_list == []
Example #12
0
def main():
    (options, args) = parse_args()

    try:
        (options.username,
         options.password) = open(options.passfile,
                                  'r').readline().rstrip('\n').split(':')
    except:
        print >> sys.stderr, "Unable to read username and password from file '%s'. "
        "Make sure the file is readable and contains a single line of "
        "the form \"<username>:<password>\"" % options.passfile

    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    try:
        cdh_api = get_root_resource(options.host, options.port,
                                    options.username, options.password,
                                    options.use_tls, CM_API_VERSION)
    except:
        return 1

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(cdh_api, hostname)
    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(cdh_api, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "hadoop.cm.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
Example #13
0
def main():
    (options, args) = parse_args()

    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    try:
        r = redis.StrictRedis(host=options.host, port=options.port, db="", password="", socket_timeout=0.5)
    except:
        return 1

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(r, hostname)
        '''
            provide fake data for master
            to avoid NOt SUPPORTED items
        '''
        if data[hostname]['redis.replication[role]'] == 'master':
            data[hostname]['redis.replication[master_last_io_seconds_ago]'] = 0
            data[hostname]['redis.replication[master_link_status]'] = 'up'
            data[hostname]['redis.replication[master_sync_in_progress]'] = 0

    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(r, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "redis.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
Example #14
0
def main():
    (options, args) = parse_args()
    data = {}
    rawjson = ""
    zbxret = 0

    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    zbx_container = protobix.DataContainer("items", options.zabbix_server,
                                           int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        '''request = urllib2.Request( ("http://%s:%d/_stats" % (options.host, int(options.port))), timeout=0.5 )'''
        request = urllib2.Request(
            ("http://%s:%d/_stats" % (options.host, int(options.port))))
        opener = urllib2.build_opener()
        rawjson = opener.open(request, None, 1)
    except urllib2.URLError as e:
        if options.debug:
            print ATS_CONN_ERR % e.reason
        return 1
    else:

        if (rawjson):
            json = simplejson.load(rawjson)
            for item in json['global']:
                if item not in ITEM_BL:
                    zbx_container.add_item(hostname, ("ats.%s" % item),
                                           json['global'][item])

        zbx_container.add_item(hostname, "ats.zbx_version", __version__)

        try:
            zbxret = zbx_container.send(zbx_container)
        except protobix.SenderException as zbx_e:
            if options.debug:
                print ZBX_CONN_ERR % zbx_e.err_text
            return 2
        else:
            return 0
    def run(self, options=None):
        # Init logging with default values since we don't have real config yet
        self._init_logging()
        # Parse command line options
        args = sys.argv[1:]
        if isinstance(options, list):
            args = options
        self.options = self._parse_args(args)

        # Get configuration
        self.zbx_config = self._init_config()

        # Update logger with configuration
        self._setup_logging(
            self.zbx_config.log_type,
            self.zbx_config.debug_level,
            self.zbx_config.log_file
        )

        # Datacontainer init
        zbx_container = protobix.DataContainer(
            config = self.zbx_config,
            logger=self.logger
        )
        # Get back hostname from ZabbixAgentConfig
        self.hostname = self.zbx_config.hostname

        # Step 1: read probe configuration
        #         initialize any needed object or connection
        self._init_probe()

        # Step 2: get data
        data = {}
        if self.options.probe_mode == "update":
            zbx_container.data_type = 'items'
            data = self._get_metrics()
        elif self.options.probe_mode == "discovery":
            zbx_container.data_type = 'lld'
            data = self._get_discovery()

        # Step 3: add data to container
        zbx_container.add(data)

        # Step 4: send data to Zabbix server
        server_success, server_failure, processed, failed, total, time = zbx_container.send()
        return  server_success, server_failure, processed, failed, total, time
Example #16
0
def test_debug_no_dryrun_no(data_type):
    """
    debug_level to False
    dryrun to False
    """
    zbx_datacontainer = protobix.DataContainer()
    # Force a Zabbix port so that test fails even if backend is present
    zbx_datacontainer.server_port = 10060
    zbx_datacontainer.data_type = data_type
    assert zbx_datacontainer.items_list == []
    zbx_datacontainer.add(DATA[data_type])
    assert len(zbx_datacontainer.items_list) == 4

    assert zbx_datacontainer.dryrun is False
    assert zbx_datacontainer.debug_level < 4
    ''' Send data to zabbix '''
    with pytest.raises(socket.error):
        results_list = zbx_datacontainer.send()
Example #17
0
def main():

    (options, args) = parse_args()
    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    try:
        rmq = RabbitMQAPI(user_name=options.username,
                          password=options.password,
                          host_name=hostname,
                          port=options.port)
    except:
        return 1

    zbx_container = protobix.DataContainer()
    data = {}
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data[hostname] = rmq.get_metrics()

    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data[hostname] = rmq.get_discovery()

    zbx_container.add(data)
    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbx_response = zbx_container.send(zbx_container)

    except protobix.SenderException as zbx_exception:
        if options.debug:
            print ZBX_CONN_ERR % zbx_exception.err_text
        return 2

    else:
        return 0
Example #18
0
    def setup_class(cls):
        common_log_format = '[%(name)s:%(levelname)s] %(message)s'
        cls.logger = logging.getLogger(cls.__class__.__name__)
        consoleHandler = logging.StreamHandler()
        consoleFormatter = logging.Formatter(fmt=common_log_format,
                                             datefmt='%Y%m%d:%H%M%S')
        consoleHandler.setFormatter(consoleFormatter)
        cls.logger.addHandler(consoleHandler)

        cls.zbx_container = protobix.DataContainer(logger=cls.logger)
        cls.zbx_container._items_list = []
        cls.zbx_container._config = {
            'server': '127.0.0.1',
            'port': 10051,
            'log_level': 3,
            'log_output': '/tmp/zabbix_agentd.log',
            'dryrun': False,
            'data_type': None,
            'timeout': 3,
        }
Example #19
0
 def zbx_sender(self, host, data, data_type='items'):
     """Function to send trapper data to Zabbix"""
     # In order to use the Zabbix Agent Config portion of protobix, I cannot simply provide
     # a config dictionary.  I have to provide a config file.  These lines will create a
     # viable, temporary config file and then delete it once the config object has been created.
     self.create_tmpfile()
     pzalogger = None
     pdclogger = None
     if self.debug:
         pzalogger = logging.getLogger('protobix.ZabbixAgentConfig')
         pdclogger = logging.getLogger('protobix.DataContainer')
     zbx_config = protobix.ZabbixAgentConfig(config_file=self.zbxconfigfile,
                                             logger=pzalogger)
     self.delete_tmpfile()
     zbx_datacontainer = protobix.DataContainer(config=zbx_config,
                                                logger=pdclogger)
     zbx_datacontainer.data_type = data_type
     self.logger.debug('DATA = {0}'.format({host: data}))
     zbx_datacontainer.add({host: data})
     return zbx_datacontainer.send()
Example #20
0
def test_need_backend_debug_no_dryrun_no(data_type):
    """
    debug_level to False
    dryrun to False
    """
    zbx_datacontainer = protobix.DataContainer()
    zbx_datacontainer.data_type = data_type
    assert zbx_datacontainer.items_list == []
    zbx_datacontainer.add(DATA[data_type])
    assert len(zbx_datacontainer.items_list) == 4

    assert zbx_datacontainer.dryrun is False
    assert zbx_datacontainer.debug_level < 4
    ''' Send data to zabbix '''
    srv_success, srv_failure, processed, failed, total, time = zbx_datacontainer.send(
    )
    assert srv_success == 1
    assert srv_failure == 0
    assert processed == 4
    assert failed == 0
    assert total == 4
    assert zbx_datacontainer.items_list == []
Example #21
0
def main():
    (options, args) = parse_args()

    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    try:
        mc = memcache.Client(["%s:%s" % (hostname, options.port)])
    except:
        return 1

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(mc, hostname)

    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(mc, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "memcached.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
Example #22
0
def test_debug_yes_dryrun_yes(data_type):
    """
    debug_level to True
    dryrun to True
    """
    zbx_datacontainer = protobix.DataContainer()
    zbx_datacontainer.data_type = data_type
    zbx_datacontainer.dryrun = True
    zbx_datacontainer.debug_level = 4

    assert zbx_datacontainer.items_list == []
    zbx_datacontainer.add(DATA[data_type])

    assert len(zbx_datacontainer.items_list) == 4
    ''' Send data to zabbix '''
    srv_success, srv_failure, processed, failed, total, time = zbx_datacontainer.send(
    )
    assert srv_success == 0
    assert srv_failure == 0
    assert processed == 0
    assert failed == 0
    assert total == 4
    assert zbx_datacontainer.items_list == []
Example #23
0
 def initSender(self, dataType='items'):
     self.zbx_config.data_type = dataType
     self.zbx_datacontainer = protobix.DataContainer(config=self.zbx_config)
     return self.zbx_datacontainer
fila = []
n=0
nome_fila = []
for key in listkeys:
        print('{"{#FILA}":"%s"},' % (key))

print('{"{#FILA}":"TotalObjetos"}')
print(']}')

##Update items.
data1 = {}
data2 = {}
totobjs=0

for key in listkeys:
  if r.type(key) == "list":
    totitens=(r.llen(key))
    kb = "fila.redis[" + key + "]"
    data2[kb] = totitens
    totobjs = totobjs + totitens


kb = 'fila.redis[TotalObjetos]'
data2[kb] = totobjs
data1[hostname] = data2
DATA = json.loads(json.dumps(data1))
zbx = protobix.DataContainer()
zbx.data_type = 'items'
zbx.add(DATA)
zbx.send()
Example #25
0
      json_list.append(('{ "%s": {"svc.discovery.pool":[') % cluster)
  
    # json_list.append('{"data":[')

    for i, v in enumerate( output ):
      if i < len(output)-1:
        json_list.append(v+',')
      else:
        json_list.append(v)
    json_list.append(']}}')

    json_string = ''.join(json_list)
    parsed = json.loads(json_string)
    debug_print(json.dumps(parsed, indent=4, sort_keys=True))
    # debug_print(json_string)

    trapper_key = 'svc.discovery.%s' % discovery
    debug_print('Sending to host=localhost, key=%s' % (trapper_key))

    #send json to LLD trapper item with zbxsend module
    if debug:
      logging.basicConfig(level=logging.DEBUG)
    else:
      logging.basicConfig(level=logging.WARNING)
    #send_to_zabbix([Metric(cluster, trapper_key, json_string)], 'localhost', 10051)
    zbx_datacontainer = protobix.DataContainer()
    zbx_datacontainer.debug_level = 5
    zbx_datacontainer.data_type = 'lld'
    zbx_datacontainer.add(parsed)
    zbx_datacontainer.send()
    debug_print('')
Example #26
0
#!/usr/bin/env python
# -*- coding:utf8 -*-
''' import module '''
import protobix
''' create DataContainer, providing data_type, zabbix server and port '''
zbx_container = protobix.DataContainer("items", "172.16.5.220", 10051)
''' set debug '''
zbx_container.set_debug(True)
zbx_container.set_verbosity(True)
''' Add items one after the other '''
hostname = "localhost"  #hostname ying shi zbx li de mingzi
item = "trap1"  #item shi shoudong chuanjian
value = "test-only"
zbx_container.add_item(hostname, item, value)
''' or use bulk insert '''
data = {
    "172.16.5.238": {
        "trap1": "test-only",
    },
    "myhost2": {
        "my.zabbix.item1": 0,
        "my.zabbix.item2": "item string"
    }
}
data = {}
zbx_container.add(data)
''' Send data to zabbix '''
ret = zbx_container.send(zbx_container)
''' If returns False, then we got a problem '''
if not ret:
    print "Ooops. Something went wrong when sending data to Zabbix"
Example #27
0
#coding:utf-8
from zabbix_manage import ConnZbx
import protobix
'''
注意protobix不要用pip install安装,版本差太大
github地址:https://github.com/jbfavre/python-zabbix
'''

zbx_server = '172.16.3.62'
zbx_token = '8bac858cfe416fc69df82d5aa4957788'
zbx_url = 'http://%s/zabbix/api_jsonrpc.php' % (zbx_server)
zbx_conn = ConnZbx(zbx_url, zbx_token)
'''
按照创建hostgroup、创建hosts、删除hosts、删除hostgroup的顺序同步
swarm中service、container的状态到zabbix,建议每分钟执行一次
'''
zbx_conn.mgr_zbx()
'''
通过导入的protobix使用zabbix sender协议发送数据
数据采集和发送,测试环境耗时2~5S,建议30~60秒执行一次
'''

data = zbx_conn.deal_with_datas()
zbx_container = protobix.DataContainer("items", zbx_server, 10051)
zbx_container.add(data)
ret = zbx_container.send(zbx_container)
if not ret:
    print "Ooops. Something went wrong when sending data to Zabbix"
Example #28
0
 def _init_container(self):
     zbx_container = protobix.DataContainer(
         data_type='items',
         zbx_host='localhost'
     )
     return zbx_container