| 1 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from kafka_influxdb.encoder import collectd_json_encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class TestCollectdJsonEncoder(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |         self.encoder = collectd_json_encoder.Encoder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     def test_encode(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         Test encoding of messages in collectd json format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         See https://github.com/mre/kafka-influxdb/issues/6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         :return: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         [{"values":[0.6],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"xx.example.internal","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"system"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         expected = ['cpu_1_percent,host=xx.example.internal,type_instance=system value=0.6 1444745144'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     def test_multiple_measurements(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         Test encoding of messages in collectd json format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         See https://github.com/mre/kafka-influxdb/issues/6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         :return: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         [{"values":[0.6],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"xx.example.internal","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"system"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         [{"values":[0.7],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"example.com","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"user"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         [{"values":[37.7],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"myhost","plugin":"cpu","plugin_instance":"0","type":"percent","type_instance":"nice"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         [{"values":[0],"dstypes":["gauge"],"dsnames":["value"],"time":1444745145.824,"interval":10.000,"host":"myhost","plugin":"cpu","plugin_instance":"0","type":"percent","type_instance":"interrupt"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         [{"values":[1.1],"dstypes":["gauge"],"dsnames":["value"],"time":1444745136.182,"interval":10.000,"host":"myhost","plugin":"memory","plugin_instance":"","type":"percent","type_instance":"slab_recl"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'cpu_1_percent,host=xx.example.internal,type_instance=system value=0.6 1444745144', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'cpu_1_percent,host=example.com,type_instance=user value=0.7 1444745144', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'cpu_0_percent,host=myhost,type_instance=nice value=37.7 1444745144', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'cpu_0_percent,host=myhost,type_instance=interrupt value=0 1444745145', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'memory_percent,host=myhost,type_instance=slab_recl value=1.1 1444745136' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     def test_invalid_messages(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         invalid_messages = [b'', b'\n', b'bla', b'foo\nbar\nbaz'] | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         for msg in invalid_messages: | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             self.assertEqual(self.encoder.encode(msg), []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def test_documentation_examples(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         [{"values":[0],"dstypes":["derive"],"dsnames":["value"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"cpu","plugin_instance":"1","type":"cpu","type_instance":"interrupt"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         expected = ['cpu_1_cpu,host=26f2fc918f50,type_instance=interrupt value=0 1436372292'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     def test_multiple_fields(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         Test supporting multiple fields in a sample | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         [{"values":[0.2, 0.3],"dstypes":["derive"],"dsnames":["cpu_usage", "mem_usage"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"sys_usage","plugin_instance":"1","type":"percent"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         [{"values":[0.2, 0.3],"dstypes":["derive"],"dsnames":["cpu_usage", "mem_usage"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"sys_usage","plugin_instance":"1","type":"percent"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         expected = ['sys_usage_1_percent,host=26f2fc918f50 cpu_usage=0.2,mem_usage=0.3 1436372292'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         temp = self.encoder.encode(msg) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |        { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          "values":  [1901474177], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          "dstypes":  ["counter"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          "dsnames":    ["value"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          "time":      1280959128, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          "interval":          10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          "host":            "leeloo.octo.it", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |          "plugin":          "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |          "plugin_instance": "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          "type":            "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          "type_instance":   "idle" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | # See https://collectd.org/wiki/index.php/JSON | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         "values":  [1901474177], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         "dstypes":  ["counter"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         "dsnames":    ["value"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         "time":      1280959128, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         "interval":          10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         "host":            "leeloo.octo.it", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         "plugin":          "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         "plugin_instance": "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         "type":            "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         "type_instance":   "idle" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | # See https://github.com/mjuenema/collectd-write_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         "dsnames": ['shorttem', 'midterm', 'longterm'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         "dstypes": ['gauge', 'gauge', 'gauge'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         "host": "localhost", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         "interval": 5.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         "plugin": "load", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         "plugin_instance": "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         "time": 1432086959.8153536, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         "type": "load", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         "type_instance": "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         "values": [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             0.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             0.01, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             0.050000000000000003 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | ] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | """ | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  |