Create influx bucket if it doesn't exist
This commit is contained in:
		
							parent
							
								
									94f5174e2f
								
							
						
					
					
						commit
						debadb56e5
					
				
					 1 changed files with 18 additions and 0 deletions
				
			
		|  | @ -1,6 +1,7 @@ | ||||||
| from sys import exit | from sys import exit | ||||||
| from logging import getLogger | from logging import getLogger | ||||||
| from requests.exceptions import ConnectionError | from requests.exceptions import ConnectionError | ||||||
|  | import influxdb_client | ||||||
| from influxdb_client import InfluxDBClient, Point | from influxdb_client import InfluxDBClient, Point | ||||||
| from influxdb_client.client.write_api import SYNCHRONOUS | from influxdb_client.client.write_api import SYNCHRONOUS | ||||||
| 
 | 
 | ||||||
|  | @ -17,6 +18,23 @@ class InfluxDB2Manager(object): | ||||||
|                                     timeout=self.server.timeout, verify_ssl=self.server.verify_ssl, ssl_ca_cert=self.server.ssl) |                                     timeout=self.server.timeout, verify_ssl=self.server.verify_ssl, ssl_ca_cert=self.server.ssl) | ||||||
|         self.influx_write_api = self.influx.write_api(write_options=SYNCHRONOUS) |         self.influx_write_api = self.influx.write_api(write_options=SYNCHRONOUS) | ||||||
| 
 | 
 | ||||||
|  |         # Create the bucket if needed | ||||||
|  | 
 | ||||||
|  |         bucket_api = self.influx.buckets_api() | ||||||
|  | 
 | ||||||
|  |         bucket = bucket_api.find_bucket_by_name(self.server.bucket) | ||||||
|  | 
 | ||||||
|  |         if bucket is None: | ||||||
|  |             self.logger.info('Creating bucket %s', self.server.bucket) | ||||||
|  | 
 | ||||||
|  |             org_api = influxdb_client.service.organizations_service.OrganizationsService(self.influx.api_client) | ||||||
|  |             orgs = org_api.get_orgs() | ||||||
|  |             for org in orgs.orgs: | ||||||
|  |                 if org.name == self.server.org: | ||||||
|  |                     my_org = org | ||||||
|  | 
 | ||||||
|  |             self.influx.buckets_api().create_bucket(bucket_name=self.server.bucket, org_id=my_org.id) | ||||||
|  | 
 | ||||||
|     def write_points(self, data): |     def write_points(self, data): | ||||||
|         d = data |         d = data | ||||||
|         self.logger.info('Writing Data to InfluxDBv2 %s', d) |         self.logger.info('Writing Data to InfluxDBv2 %s', d) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue