Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017, Ansible Project
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: logentries_msg
11 short_description: Send a message to logentries
12 description:
13 - Send a message to logentries.
14 extends_documentation_fragment:
15 - community.general._attributes
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 token:
23 type: str
24 description:
25 - Log token.
26 required: true
27 msg:
28 type: str
29 description:
30 - The message body.
31 required: true
32 api:
33 type: str
34 description:
35 - API endpoint.
36 default: data.logentries.com
37 port:
38 type: int
39 description:
40 - API endpoint port.
41 default: 80
42 author: "Jimmy Tang (@jcftang) <[email protected]>"
43 """
44
45 RETURN = """#"""
46
47 EXAMPLES = r"""
48 - name: Send a message to logentries
49 community.general.logentries_msg:
50 token: 00000000-0000-0000-0000-000000000000
51 msg: "{{ ansible_hostname }}"
52 """
53
54 import socket
55
56 from ansible.module_utils.basic import AnsibleModule
57
58
59 def send_msg(module, token, msg, api, port):
60 message = f"{token} {msg}\n"
61
62 api_ip = socket.gethostbyname(api)
63
64 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65 s.connect((api_ip, port))
66 try:
67 if not module.check_mode:
68 s.send(message)
69 except Exception as e:
70 module.fail_json(msg=f"failed to send message, msg={e}")
71 s.close()
72
73
74 def main():
75 module = AnsibleModule(
76 argument_spec=dict(
77 token=dict(type="str", required=True, no_log=True),
78 msg=dict(type="str", required=True),
79 api=dict(type="str", default="data.logentries.com"),
80 port=dict(type="int", default=80),
81 ),
82 supports_check_mode=True,
83 )
84
85 token = module.params["token"]
86 msg = module.params["msg"]
87 api = module.params["api"]
88 port = module.params["port"]
89
90 changed = False
91 try:
92 send_msg(module, token, msg, api, port)
93 changed = True
94 except Exception as e:
95 module.fail_json(msg=f"unable to send msg: {e}")
96
97 module.exit_json(changed=changed, msg=msg)
98
99
100 if __name__ == "__main__":
101 main()