We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Programming errors in the InfluxDB plugin
Something definitely seems wrong with the InfluxDB output plugin... I can see at least two possible problems:

1. The error handling

```python
        except InfluxDBClientError as e:
            log.msg("output_influx: I/O error({0}): '{1}'".format(
                e.errno, e.strerror))
            return
```

seems wrong to me. If you inspect the source of the Python [InfluxDB client](https://github.com/influxdata/influxdb-python/blob/master/influxdb/exceptions.py), the `InfluxDBClientError` class does not have `errno` and `strerror` fields. I think the plugin should use `e.code` and `e.message` instead respectively.

2. In the plugin, there is some code like this:

```python
        m = {
            'measurement': eventid.replace('.', '_'),
            'tags': {
                'session': entry['session'],
                'src_ip': entry['src_ip']
            },
            'fields': {
                'sensor': self.sensor
            },
        }
```

However `self.sensor` is not defined anywhere. I think it should be `entry['sensor']` instead.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/cowrie/output/influx.py`
Content:
```
1 import re
2 
3 from influxdb import InfluxDBClient
4 from influxdb.exceptions import InfluxDBClientError
5 
6 from twisted.python import log
7 
8 import cowrie.core.output
9 from cowrie.core.config import CowrieConfig
10 
11 
12 class Output(cowrie.core.output.Output):
13     """
14     influx output
15     """
16     def start(self):
17         host = CowrieConfig().get('output_influx', 'host', fallback='')
18         port = CowrieConfig().getint('output_influx', 'port', fallback=8086)
19         ssl = CowrieConfig().getboolean('output_influx', 'ssl', fallback=False)
20 
21         self.client = None
22         try:
23             self.client = InfluxDBClient(host=host, port=port, ssl=ssl, verify_ssl=ssl)
24         except InfluxDBClientError as e:
25             log.msg("output_influx: I/O error({0}): '{1}'".format(
26                 e.errno, e.strerror))
27             return
28 
29         if self.client is None:
30             log.msg("output_influx: cannot instantiate client!")
31             return
32 
33         if (CowrieConfig().has_option('output_influx', 'username') and
34                 CowrieConfig().has_option('output_influx', 'password')):
35             username = CowrieConfig().get('output_influx', 'username')
36             password = CowrieConfig().get('output_influx', 'password', raw=True)
37             self.client.switch_user(username, password)
38 
39         try:
40             dbname = CowrieConfig().get('output_influx', 'database_name')
41         except Exception:
42             dbname = 'cowrie'
43 
44         retention_policy_duration_default = '12w'
45         retention_policy_name = dbname + "_retention_policy"
46 
47         if CowrieConfig().has_option('output_influx', 'retention_policy_duration'):
48             retention_policy_duration = CowrieConfig().get(
49                 'output_influx', 'retention_policy_duration')
50 
51             match = re.search(r'^\d+[dhmw]{1}$', retention_policy_duration)
52             if not match:
53                 log.msg(("output_influx: invalid retention policy."
54                          "Using default '{}'..").format(
55                     retention_policy_duration))
56                 retention_policy_duration = retention_policy_duration_default
57         else:
58             retention_policy_duration = retention_policy_duration_default
59 
60         database_list = self.client.get_list_database()
61         dblist = [str(elem['name']) for elem in database_list]
62 
63         if dbname not in dblist:
64             self.client.create_database(dbname)
65             self.client.create_retention_policy(
66                 retention_policy_name, retention_policy_duration, 1,
67                 database=dbname, default=True)
68         else:
69             retention_policies_list = self.client.get_list_retention_policies(
70                 database=dbname)
71             rplist = [str(elem['name']) for elem in retention_policies_list]
72             if retention_policy_name not in rplist:
73                 self.client.create_retention_policy(
74                     retention_policy_name, retention_policy_duration, 1,
75                     database=dbname, default=True)
76             else:
77                 self.client.alter_retention_policy(
78                     retention_policy_name, database=dbname,
79                     duration=retention_policy_duration,
80                     replication=1, default=True)
81 
82         self.client.switch_database(dbname)
83 
84     def stop(self):
85         pass
86 
87     def write(self, entry):
88         if self.client is None:
89             log.msg("output_influx: client object is not instantiated")
90             return
91 
92         # event id
93         eventid = entry['eventid']
94 
95         # measurement init
96         m = {
97             'measurement': eventid.replace('.', '_'),
98             'tags': {
99                 'session': entry['session'],
100                 'src_ip': entry['src_ip']
101             },
102             'fields': {
103                 'sensor': self.sensor
104             },
105         }
106 
107         # event parsing
108         if eventid in ['cowrie.command.failed',
109                        'cowrie.command.input']:
110             m['fields'].update({
111                 'input': entry['input'],
112             })
113 
114         elif eventid == 'cowrie.session.connect':
115             m['fields'].update({
116                 'protocol': entry['protocol'],
117                 'src_port': entry['src_port'],
118                 'dst_port': entry['dst_port'],
119                 'dst_ip': entry['dst_ip'],
120             })
121 
122         elif eventid in ['cowrie.login.success', 'cowrie.login.failed']:
123             m['fields'].update({
124                 'username': entry['username'],
125                 'password': entry['password'],
126             })
127 
128         elif eventid == 'cowrie.session.file_download':
129             m['fields'].update({
130                 'shasum': entry.get('shasum'),
131                 'url': entry.get('url'),
132                 'outfile': entry.get('outfile')
133             })
134 
135         elif eventid == 'cowrie.session.file_download.failed':
136             m['fields'].update({
137                 'url': entry.get('url')
138             })
139 
140         elif eventid == 'cowrie.session.file_upload':
141             m['fields'].update({
142                 'shasum': entry.get('shasum'),
143                 'outfile': entry.get('outfile'),
144             })
145 
146         elif eventid == 'cowrie.session.closed':
147             m['fields'].update({
148                 'duration': entry['duration']
149             })
150 
151         elif eventid == 'cowrie.client.version':
152             m['fields'].update({
153                 'version': ','.join(entry['version']),
154             })
155 
156         elif eventid == 'cowrie.client.kex':
157             m['fields'].update({
158                 'maccs': ','.join(entry['macCS']),
159                 'kexalgs': ','.join(entry['kexAlgs']),
160                 'keyalgs': ','.join(entry['keyAlgs']),
161                 'compcs': ','.join(entry['compCS']),
162                 'enccs': ','.join(entry['encCS'])
163             })
164 
165         elif eventid == 'cowrie.client.size':
166             m['fields'].update({
167                 'height': entry['height'],
168                 'width': entry['width'],
169             })
170 
171         elif eventid == 'cowrie.client.var':
172             m['fields'].update({
173                 'name': entry['name'],
174                 'value': entry['value'],
175             })
176 
177         elif eventid == 'cowrie.client.fingerprint':
178             m['fields'].update({
179                 'fingerprint': entry['fingerprint']
180             })
181 
182             # cowrie.direct-tcpip.data, cowrie.direct-tcpip.request
183             # cowrie.log.closed
184             # are not implemented
185         else:
186             # other events should be handled
187             log.msg(
188                 "output_influx: event '{}' not handled. Skipping..".format(
189                     eventid))
190             return
191 
192         result = self.client.write_points([m])
193 
194         if not result:
195             log.msg("output_influx: error when writing '{}' measurement"
196                     "in the db.".format(eventid))
197
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/cowrie/output/influx.py b/src/cowrie/output/influx.py
--- a/src/cowrie/output/influx.py
+++ b/src/cowrie/output/influx.py
@@ -23,7 +23,7 @@
             self.client = InfluxDBClient(host=host, port=port, ssl=ssl, verify_ssl=ssl)
         except InfluxDBClientError as e:
             log.msg("output_influx: I/O error({0}): '{1}'".format(
-                e.errno, e.strerror))
+                e.code, e.message))
             return
 
         if self.client is None:

{"golden_diff": "diff --git a/src/cowrie/output/influx.py b/src/cowrie/output/influx.py\n--- a/src/cowrie/output/influx.py\n+++ b/src/cowrie/output/influx.py\n@@ -23,7 +23,7 @@\n             self.client = InfluxDBClient(host=host, port=port, ssl=ssl, verify_ssl=ssl)\n         except InfluxDBClientError as e:\n             log.msg(\"output_influx: I/O error({0}): '{1}'\".format(\n-                e.errno, e.strerror))\n+                e.code, e.message))\n             return\n \n         if self.client is None:\n", "issue": "Programming errors in the InfluxDB plugin\nSomething definitely seems wrong with the InfluxDB output plugin... I can see at least two possible problems:\r\n\r\n1. The error handling\r\n\r\n```python\r\n        except InfluxDBClientError as e:\r\n            log.msg(\"output_influx: I/O error({0}): '{1}'\".format(\r\n                e.errno, e.strerror))\r\n            return\r\n```\r\n\r\nseems wrong to me. If you inspect the source of the Python [InfluxDB client](https://github.com/influxdata/influxdb-python/blob/master/influxdb/exceptions.py), the `InfluxDBClientError` class does not have `errno` and `strerror` fields. I think the plugin should use `e.code` and `e.message` instead respectively.\r\n\r\n2. In the plugin, there is some code like this:\r\n\r\n```python\r\n        m = {\r\n            'measurement': eventid.replace('.', '_'),\r\n            'tags': {\r\n                'session': entry['session'],\r\n                'src_ip': entry['src_ip']\r\n            },\r\n            'fields': {\r\n                'sensor': self.sensor\r\n            },\r\n        }\r\n```\r\n\r\nHowever `self.sensor` is not defined anywhere. I think it should be `entry['sensor']` instead.\n", "before_files": [{"content": "import re\n\nfrom influxdb import InfluxDBClient\nfrom influxdb.exceptions import InfluxDBClientError\n\nfrom twisted.python import log\n\nimport cowrie.core.output\nfrom cowrie.core.config import CowrieConfig\n\n\nclass Output(cowrie.core.output.Output):\n    \"\"\"\n    influx output\n    \"\"\"\n    def start(self):\n        host = CowrieConfig().get('output_influx', 'host', fallback='')\n        port = CowrieConfig().getint('output_influx', 'port', fallback=8086)\n        ssl = CowrieConfig().getboolean('output_influx', 'ssl', fallback=False)\n\n        self.client = None\n        try:\n            self.client = InfluxDBClient(host=host, port=port, ssl=ssl, verify_ssl=ssl)\n        except InfluxDBClientError as e:\n            log.msg(\"output_influx: I/O error({0}): '{1}'\".format(\n                e.errno, e.strerror))\n            return\n\n        if self.client is None:\n            log.msg(\"output_influx: cannot instantiate client!\")\n            return\n\n        if (CowrieConfig().has_option('output_influx', 'username') and\n                CowrieConfig().has_option('output_influx', 'password')):\n            username = CowrieConfig().get('output_influx', 'username')\n            password = CowrieConfig().get('output_influx', 'password', raw=True)\n            self.client.switch_user(username, password)\n\n        try:\n            dbname = CowrieConfig().get('output_influx', 'database_name')\n        except Exception:\n            dbname = 'cowrie'\n\n        retention_policy_duration_default = '12w'\n        retention_policy_name = dbname + \"_retention_policy\"\n\n        if CowrieConfig().has_option('output_influx', 'retention_policy_duration'):\n            retention_policy_duration = CowrieConfig().get(\n                'output_influx', 'retention_policy_duration')\n\n            match = re.search(r'^\\d+[dhmw]{1}$', retention_policy_duration)\n            if not match:\n                log.msg((\"output_influx: invalid retention policy.\"\n                         \"Using default '{}'..\").format(\n                    retention_policy_duration))\n                retention_policy_duration = retention_policy_duration_default\n        else:\n            retention_policy_duration = retention_policy_duration_default\n\n        database_list = self.client.get_list_database()\n        dblist = [str(elem['name']) for elem in database_list]\n\n        if dbname not in dblist:\n            self.client.create_database(dbname)\n            self.client.create_retention_policy(\n                retention_policy_name, retention_policy_duration, 1,\n                database=dbname, default=True)\n        else:\n            retention_policies_list = self.client.get_list_retention_policies(\n                database=dbname)\n            rplist = [str(elem['name']) for elem in retention_policies_list]\n            if retention_policy_name not in rplist:\n                self.client.create_retention_policy(\n                    retention_policy_name, retention_policy_duration, 1,\n                    database=dbname, default=True)\n            else:\n                self.client.alter_retention_policy(\n                    retention_policy_name, database=dbname,\n                    duration=retention_policy_duration,\n                    replication=1, default=True)\n\n        self.client.switch_database(dbname)\n\n    def stop(self):\n        pass\n\n    def write(self, entry):\n        if self.client is None:\n            log.msg(\"output_influx: client object is not instantiated\")\n            return\n\n        # event id\n        eventid = entry['eventid']\n\n        # measurement init\n        m = {\n            'measurement': eventid.replace('.', '_'),\n            'tags': {\n                'session': entry['session'],\n                'src_ip': entry['src_ip']\n            },\n            'fields': {\n                'sensor': self.sensor\n            },\n        }\n\n        # event parsing\n        if eventid in ['cowrie.command.failed',\n                       'cowrie.command.input']:\n            m['fields'].update({\n                'input': entry['input'],\n            })\n\n        elif eventid == 'cowrie.session.connect':\n            m['fields'].update({\n                'protocol': entry['protocol'],\n                'src_port': entry['src_port'],\n                'dst_port': entry['dst_port'],\n                'dst_ip': entry['dst_ip'],\n            })\n\n        elif eventid in ['cowrie.login.success', 'cowrie.login.failed']:\n            m['fields'].update({\n                'username': entry['username'],\n                'password': entry['password'],\n            })\n\n        elif eventid == 'cowrie.session.file_download':\n            m['fields'].update({\n                'shasum': entry.get('shasum'),\n                'url': entry.get('url'),\n                'outfile': entry.get('outfile')\n            })\n\n        elif eventid == 'cowrie.session.file_download.failed':\n            m['fields'].update({\n                'url': entry.get('url')\n            })\n\n        elif eventid == 'cowrie.session.file_upload':\n            m['fields'].update({\n                'shasum': entry.get('shasum'),\n                'outfile': entry.get('outfile'),\n            })\n\n        elif eventid == 'cowrie.session.closed':\n            m['fields'].update({\n                'duration': entry['duration']\n            })\n\n        elif eventid == 'cowrie.client.version':\n            m['fields'].update({\n                'version': ','.join(entry['version']),\n            })\n\n        elif eventid == 'cowrie.client.kex':\n            m['fields'].update({\n                'maccs': ','.join(entry['macCS']),\n                'kexalgs': ','.join(entry['kexAlgs']),\n                'keyalgs': ','.join(entry['keyAlgs']),\n                'compcs': ','.join(entry['compCS']),\n                'enccs': ','.join(entry['encCS'])\n            })\n\n        elif eventid == 'cowrie.client.size':\n            m['fields'].update({\n                'height': entry['height'],\n                'width': entry['width'],\n            })\n\n        elif eventid == 'cowrie.client.var':\n            m['fields'].update({\n                'name': entry['name'],\n                'value': entry['value'],\n            })\n\n        elif eventid == 'cowrie.client.fingerprint':\n            m['fields'].update({\n                'fingerprint': entry['fingerprint']\n            })\n\n            # cowrie.direct-tcpip.data, cowrie.direct-tcpip.request\n            # cowrie.log.closed\n            # are not implemented\n        else:\n            # other events should be handled\n            log.msg(\n                \"output_influx: event '{}' not handled. Skipping..\".format(\n                    eventid))\n            return\n\n        result = self.client.write_points([m])\n\n        if not result:\n            log.msg(\"output_influx: error when writing '{}' measurement\"\n                    \"in the db.\".format(eventid))\n", "path": "src/cowrie/output/influx.py"}], "after_files": [{"content": "import re\n\nfrom influxdb import InfluxDBClient\nfrom influxdb.exceptions import InfluxDBClientError\n\nfrom twisted.python import log\n\nimport cowrie.core.output\nfrom cowrie.core.config import CowrieConfig\n\n\nclass Output(cowrie.core.output.Output):\n    \"\"\"\n    influx output\n    \"\"\"\n    def start(self):\n        host = CowrieConfig().get('output_influx', 'host', fallback='')\n        port = CowrieConfig().getint('output_influx', 'port', fallback=8086)\n        ssl = CowrieConfig().getboolean('output_influx', 'ssl', fallback=False)\n\n        self.client = None\n        try:\n            self.client = InfluxDBClient(host=host, port=port, ssl=ssl, verify_ssl=ssl)\n        except InfluxDBClientError as e:\n            log.msg(\"output_influx: I/O error({0}): '{1}'\".format(\n                e.code, e.message))\n            return\n\n        if self.client is None:\n            log.msg(\"output_influx: cannot instantiate client!\")\n            return\n\n        if (CowrieConfig().has_option('output_influx', 'username') and\n                CowrieConfig().has_option('output_influx', 'password')):\n            username = CowrieConfig().get('output_influx', 'username')\n            password = CowrieConfig().get('output_influx', 'password', raw=True)\n            self.client.switch_user(username, password)\n\n        try:\n            dbname = CowrieConfig().get('output_influx', 'database_name')\n        except Exception:\n            dbname = 'cowrie'\n\n        retention_policy_duration_default = '12w'\n        retention_policy_name = dbname + \"_retention_policy\"\n\n        if CowrieConfig().has_option('output_influx', 'retention_policy_duration'):\n            retention_policy_duration = CowrieConfig().get(\n                'output_influx', 'retention_policy_duration')\n\n            match = re.search(r'^\\d+[dhmw]{1}$', retention_policy_duration)\n            if not match:\n                log.msg((\"output_influx: invalid retention policy.\"\n                         \"Using default '{}'..\").format(\n                    retention_policy_duration))\n                retention_policy_duration = retention_policy_duration_default\n        else:\n            retention_policy_duration = retention_policy_duration_default\n\n        database_list = self.client.get_list_database()\n        dblist = [str(elem['name']) for elem in database_list]\n\n        if dbname not in dblist:\n            self.client.create_database(dbname)\n            self.client.create_retention_policy(\n                retention_policy_name, retention_policy_duration, 1,\n                database=dbname, default=True)\n        else:\n            retention_policies_list = self.client.get_list_retention_policies(\n                database=dbname)\n            rplist = [str(elem['name']) for elem in retention_policies_list]\n            if retention_policy_name not in rplist:\n                self.client.create_retention_policy(\n                    retention_policy_name, retention_policy_duration, 1,\n                    database=dbname, default=True)\n            else:\n                self.client.alter_retention_policy(\n                    retention_policy_name, database=dbname,\n                    duration=retention_policy_duration,\n                    replication=1, default=True)\n\n        self.client.switch_database(dbname)\n\n    def stop(self):\n        pass\n\n    def write(self, entry):\n        if self.client is None:\n            log.msg(\"output_influx: client object is not instantiated\")\n            return\n\n        # event id\n        eventid = entry['eventid']\n\n        # measurement init\n        m = {\n            'measurement': eventid.replace('.', '_'),\n            'tags': {\n                'session': entry['session'],\n                'src_ip': entry['src_ip']\n            },\n            'fields': {\n                'sensor': self.sensor\n            },\n        }\n\n        # event parsing\n        if eventid in ['cowrie.command.failed',\n                       'cowrie.command.input']:\n            m['fields'].update({\n                'input': entry['input'],\n            })\n\n        elif eventid == 'cowrie.session.connect':\n            m['fields'].update({\n                'protocol': entry['protocol'],\n                'src_port': entry['src_port'],\n                'dst_port': entry['dst_port'],\n                'dst_ip': entry['dst_ip'],\n            })\n\n        elif eventid in ['cowrie.login.success', 'cowrie.login.failed']:\n            m['fields'].update({\n                'username': entry['username'],\n                'password': entry['password'],\n            })\n\n        elif eventid == 'cowrie.session.file_download':\n            m['fields'].update({\n                'shasum': entry.get('shasum'),\n                'url': entry.get('url'),\n                'outfile': entry.get('outfile')\n            })\n\n        elif eventid == 'cowrie.session.file_download.failed':\n            m['fields'].update({\n                'url': entry.get('url')\n            })\n\n        elif eventid == 'cowrie.session.file_upload':\n            m['fields'].update({\n                'shasum': entry.get('shasum'),\n                'outfile': entry.get('outfile'),\n            })\n\n        elif eventid == 'cowrie.session.closed':\n            m['fields'].update({\n                'duration': entry['duration']\n            })\n\n        elif eventid == 'cowrie.client.version':\n            m['fields'].update({\n                'version': ','.join(entry['version']),\n            })\n\n        elif eventid == 'cowrie.client.kex':\n            m['fields'].update({\n                'maccs': ','.join(entry['macCS']),\n                'kexalgs': ','.join(entry['kexAlgs']),\n                'keyalgs': ','.join(entry['keyAlgs']),\n                'compcs': ','.join(entry['compCS']),\n                'enccs': ','.join(entry['encCS'])\n            })\n\n        elif eventid == 'cowrie.client.size':\n            m['fields'].update({\n                'height': entry['height'],\n                'width': entry['width'],\n            })\n\n        elif eventid == 'cowrie.client.var':\n            m['fields'].update({\n                'name': entry['name'],\n                'value': entry['value'],\n            })\n\n        elif eventid == 'cowrie.client.fingerprint':\n            m['fields'].update({\n                'fingerprint': entry['fingerprint']\n            })\n\n            # cowrie.direct-tcpip.data, cowrie.direct-tcpip.request\n            # cowrie.log.closed\n            # are not implemented\n        else:\n            # other events should be handled\n            log.msg(\n                \"output_influx: event '{}' not handled. Skipping..\".format(\n                    eventid))\n            return\n\n        result = self.client.write_points([m])\n\n        if not result:\n            log.msg(\"output_influx: error when writing '{}' measurement\"\n                    \"in the db.\".format(eventid))\n", "path": "src/cowrie/output/influx.py"}]}