Debian Codegraph @

ansible_collections/community/general/plugins/modules/influxdb_query.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, René Moser <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: influxdb_query
10 short_description: Query data points from InfluxDB
11 description:
12 - Query data points from InfluxDB.
13 author: "René Moser (@resmo)"
14 requirements:
15 - "influxdb >= 0.9"
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 query:
23 description:
24 - Query to be executed.
25 required: true
26 type: str
27 database_name:
28 description:
29 - Name of the database.
30 required: true
31 type: str
32 extends_documentation_fragment:
33 - community.general._influxdb
34 - community.general._attributes
35 """
36
37 EXAMPLES = r"""
38 - name: Query connections
39 community.general.influxdb_query:
40 hostname: "{{ influxdb_ip_address }}"
41 database_name: "{{ influxdb_database_name }}"
42 query: "select mean(value) from connections"
43 register: connection
44
45 - name: Query connections with tags filters
46 community.general.influxdb_query:
47 hostname: "{{ influxdb_ip_address }}"
48 database_name: "{{ influxdb_database_name }}"
49 query: "select mean(value) from connections where region='zue01' and host='server01'"
50 register: connection
51
52 - name: Print results from the query
53 ansible.builtin.debug:
54 var: connection.query_results
55 """
56
57 RETURN = r"""
58 query_results:
59 description: Result from the query.
60 returned: success
61 type: list
62 sample:
63 - mean: 1245.5333333333333
64 time: "1970-01-01T00:00:00Z"
65 """
66
67 from ansible.module_utils.basic import AnsibleModule
68
69 from ansible_collections.community.general.plugins.module_utils._influxdb import InfluxDb
70
71
72 class AnsibleInfluxDBRead(InfluxDb):
73 def read_by_query(self, query):
74 client = self.connect_to_influxdb()
75 try:
76 rs = client.query(query)
77 if rs:
78 return list(rs.get_points())
79 except Exception as e:
80 self.module.fail_json(msg=f"{e}")
81
82
83 def main():
84 argument_spec = InfluxDb.influxdb_argument_spec()
85 argument_spec.update(
86 query=dict(type="str", required=True),
87 database_name=dict(required=True, type="str"),
88 )
89 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
90
91 influx = AnsibleInfluxDBRead(module)
92 query = module.params.get("query")
93 results = influx.read_by_query(query)
94 module.exit_json(changed=True, query_results=results)
95
96
97 if __name__ == "__main__":
98 main()