def test_batch(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['batch'] = 2 metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') metric2 = Metric('servers.myhostname.cpu.cpu_time', 123, raw_value=456, timestamp=5678910, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) handler.process(metric2) body = ( '[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' '123, "tags": {"hostname": "myhostname"}}, {"timestamp": 567891' '0, "metric": "cpu.cpu_time", "value": 123, "tags": {"hostname"' ': "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_haproxy_metrics(self, mock_urlopen, mock_request): """ taghandling deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric( 'servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' 'bin', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "haproxy.SOME-BACKEND.SOME-' 'SERVER.bin", "value": 123, "tags": {"myFirstTag": "myValue", ' '"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_cpu_metrics_taghandling_2(self, mock_urlopen, mock_request): """ aggregate deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = True config['skipAggregates'] = False metric = Metric('servers.myhostname.cpu.total.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.total.user", "value": ' '123, "tags": {"cpuId": "total", "myFirstTag": "myValue", ' '"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_compression(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['compression'] = 1 metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "cpu.cpu_count", "value": 123, "tags": { "hostname": "myhostname" } }] passed_headers = mock_request.call_args[0][2] passed_body = mock_request.call_args[0][1] assert passed_headers['Content-Encoding'] == 'gzip' assert passed_headers['Content-Type'] == 'application/json' assert json.loads(self.decompress(passed_body)) == body
def test_diskspace_metrics(self, mock_urlopen, mock_request): """ taghandling deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric( 'servers.myhostname.diskspace.MOUNT_POINT.byte_' 'percentfree', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "diskspace.MOUNT_POINT' '.byte_percentfree", "value": 80, "tags": {"myFirstTag": ' '"myValue", "hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_iostat_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "iostat.io_in_progress", "value": 80, "tags": { "device": "DEV", "myFirstTag": "myValue", "hostname": "myhostname" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_network_metrics(self, mock_request, mock_urlopen): """ taghandling deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric('servers.myhostname.network.IF.rx_packets', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "network.IF.rx_packets", "value": 80, "tags": { "myFirstTag": "myValue", "hostname": "myhostname" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_diskspace_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric( 'servers.myhostname.diskspace.MOUNT_POINT.byte_percent' 'free', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "diskspace.byte_percentfree", "value": 80, "tags": { "mountpoint": "MOUNT_POINT", "myFirstTag": "myValue", "hostname": "myhostname" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_haproxy_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric( 'servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' 'bin', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "haproxy.bin", "value": 123, "tags": { "backend": "SOME-BACKEND", "myFirstTag": "myValue", "hostname": "myhostname", "server": "SOME-SERVER" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_cpu_metrics_taghandling_1(self, mock_request, mock_urlopen): """ aggregate deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric('servers.myhostname.cpu.total.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "cpu.total.user", "value": 123, "tags": { "myFirstTag": "myValue", "hostname": "myhostname" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_tags(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = 'tag1=tagv1 tag2=tagv2' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "cpu.cpu_count", "value": 123, "tags": { "hostname": "myhostname", "tag1": "tagv1", "tag2": "tagv2" } }] header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header)
def test_user_password(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['user'] = '******' config['password'] = '******' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = [{ "timestamp": 1234567, "metric": "cpu.cpu_count", "value": 123, "tags": { "hostname": "myhostname" } }] header = { 'Content-Type': 'application/json', 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5' } self.check_request_param(mock_request, body, header)
def test_HTTPError(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) header = {'Content-Type': 'application/json'} exception = urllib2.HTTPError(url=self.url, code=404, msg="Error", hdrs=header, fp=None) handler.side_effect = exception handler.process(metric)
def test_single_metric(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' '123, "tags": {"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_prefix(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['prefix'] = 'diamond' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "diamond.cpu.cpu_count", ' '"value": 123, "tags": {"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_user_password(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['user'] = '******' config['password'] = '******' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' '123, "tags": {"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json', 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5'} mock_urlopen.assert_called_with(self.url, body, header)
def test_cpu_metrics_taghandling_default2(self, mock_urlopen, mock_request): """ aggregate default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.cpu.total.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) assert not mock_urlopen.called, "should not process"
def test_compression(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['compression'] = 1 metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' '123, "tags": {"hostname": "myhostname"}}]') passed_headers = mock_urlopen.call_args[0][2] passed_body = mock_urlopen.call_args[0][1] assert passed_headers['Content-Encoding'] == 'gzip' assert passed_headers['Content-Type'] == 'application/json' assert self.decompress(passed_body) == body
def test_cpu_metrics_taghandling_default(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.cpu.cpu0.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.user", "value": ' '123, "tags": {"cpuId": "cpu0", "myFirstTag": "myValue", ' '"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_single_gauge(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = ('put cpu.cpu_count 1234567 123 hostname=myhostname\n') handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_compression(self, mock_urlopen, mock_request): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['compression'] = 1 metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' '123, "tags": {"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} assert self.decompress(mock_urlopen.call_args[0][1]) == body
def test_with_multiple_tag_no_space_in_front(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = 'myTag=myValue mySecondTag=myOtherValue' metric = Metric('servers.myhostname.cpu.cpu_count', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put cpu.cpu_count 1234567 123 hostname=myhostname ' expected_data += 'myTag=myValue mySecondTag=myOtherValue\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_cpu_metrics_taghandling_aggregate_default(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.cpu.total.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put cpu.user 1234567 123 hostname=myhostname ' expected_data += 'myFirstTag=myValue cpuId=cpu0\n' handler = TSDBHandler(config) handler.process(metric) assert not handler.socket.sendall.called, "should not process"
def test_network_metrics_default(self, mock_urlopen, mock_request): """ taghandling default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.network.IF.rx_packets', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "network.rx_packets", ' '"value": 80, "tags": {"interface": "IF", "myFirstTag": ' '"myValue", "hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_cpu_metrics_taghandling_aggregate_deactivate_so_old_values1(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric('servers.myhostname.cpu.total.user', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put cpu.total.user 1234567 123 hostname=myhostname' expected_data += ' myFirstTag=myValue\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_iostat_metrics(self, mock_urlopen, mock_request): """ taghandling deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "iostat.DEV.io_in_progress"' ', "value": 80, "tags": {"myFirstTag": "myValue", "hostname": ' '"myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_haproxy_metrics(self, mock_urlopen, mock_request): """ taghandling deactivate """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] config['cleanMetrics'] = False metric = Metric('servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' 'bin', 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "haproxy.SOME-BACKEND.SOME-' 'SERVER.bin", "value": 123, "tags": {"myFirstTag": "myValue", ' '"hostname": "myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_diskspace_metrics_default(self, mock_urlopen, mock_request): """ taghandling default """ config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' config['tags'] = ['myFirstTag=myValue'] metric = Metric('servers.myhostname.diskspace.MOUNT_POINT.byte_percent' 'free', 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) body = ('[{"timestamp": 1234567, "metric": "diskspace.' 'byte_percentfree", "value": 80, "tags": {"mountpoint": ' '"MOUNT_POINT", "myFirstTag": "myValue", "hostname": ' '"myhostname"}}]') header = {'Content-Type': 'application/json'} mock_urlopen.assert_called_with(self.url, body, header)
def test_haproxy_metrics_taghandling_default(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] metricName = 'servers.myhostname.' metricName += 'haproxy.SOME-BACKEND.SOME-SERVER.bin' metric = Metric(metricName, 123, raw_value=123, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put haproxy.bin 1234567 123 hostname=myhostname ' expected_data += 'myFirstTag=myValue server=SOME-SERVER ' expected_data += 'backend=SOME-BACKEND\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_diskspace_metrics_taghandling_default(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] metricName = 'servers.myhostname.' metricName += 'diskspace.MOUNT_POINT.byte_percentfree' metric = Metric(metricName, 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put diskspace.byte_percentfree 1234567 80 ' expected_data += 'hostname=myhostname ' expected_data += 'myFirstTag=myValue mountpoint=MOUNT_POINT\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_iostat_metrics_taghandling_default(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] metricName = 'servers.myhostname.' metricName += 'iostat.DEV.io_in_progress' metric = Metric(metricName, 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put iostat.io_in_progress 1234567 80 ' expected_data += 'hostname=myhostname ' expected_data += 'myFirstTag=myValue device=DEV\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)
def test_network_metrics_taghandling_default(self): config = configobj.ConfigObj() config['host'] = 'localhost' config['port'] = '9999' config['tags'] = ['myFirstTag=myValue'] metricName = 'servers.myhostname.' metricName += 'network.IF.rx_packets' metric = Metric(metricName, 80, raw_value=80, timestamp=1234567, host='myhostname', metric_type='GAUGE') expected_data = 'put network.rx_packets 1234567 80 ' expected_data += 'hostname=myhostname ' expected_data += 'myFirstTag=myValue interface=IF\n' handler = TSDBHandler(config) handler.process(metric) handler.socket.sendall.assert_called_with(expected_data)