UTI: Export results
[csit.git] / resources / libraries / python / model / ExportLog.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Module with keywords that publish metric and other log events.
15 """
16
17 import datetime
18
19 from resources.libraries.python.model.util import get_export_data
20
21
22 def export_ssh_command(host, port, command):
23     """Add a log item about SSH command execution starting.
24
25     The log item is present only in raw output.
26     Result arrives in a separate log item.
27     Log level is always DEBUG.
28
29     The command is stored as "data" (not "msg") as in some cases
30     the command can be too long to act as a message.
31
32     The host is added to the info set of hosts.
33
34     :param host: Node "host" attribute, usually its IPv4 address.
35     :param port: SSH port number to use when connecting to the host.
36     :param command: Serialized bash command to execute.
37     :type host: str
38     :type port: int
39     :type command: str
40     """
41     timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
42     data = get_export_data()
43     ssh_record = dict(
44         source_type=u"host,port",
45         source_id=dict(host=host, port=port),
46         msg_type=u"ssh_command",
47         log_level=u"DEBUG",
48         timestamp=timestamp,
49         msg="",
50         data=str(command),
51     )
52     data[u"hosts"].add(host)
53     data[u"log"].append(ssh_record)
54
55
56 def export_ssh_result(host, port, code, stdout, stderr, duration):
57     """Add a log item about ssh execution result.
58
59     Only for raw output log.
60
61     There is no easy way to pair with the corresponding command,
62     but usually there is only one SSH session for given host and port.
63     The duration value may give a hint if that is not the case.
64
65     Message is empty, data has fields "rc", "stdout", "stderr" and "duration".
66     Log level is always DEBUG.
67
68     The host is NOT added to the info set of hosts, as each result
69     comes after a command.
70
71     TODO: Do not require duration, find preceding ssh command in log.
72     Reason: Pylint complains about too many arguments.
73     Alternative: Define type for SSH endopoint (and use that instead host+port).
74
75     :param host: Node "host" attribute, usually its IPv4 address.
76     :param port: SSH port number to use when connecting to the host.
77     :param code: Bash return code, e.g. 0 for success.
78     :param stdout: Captured standard output of the command execution.
79     :param stderr: Captured error output of the command execution.
80     :param duration: How long has the command been executing, in seconds.
81     :type host: str
82     :type port: int
83     :type code: int
84     :type stdout: str
85     :type stderr: str
86     :type duration: float
87     """
88     timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
89     data = get_export_data()
90     ssh_record = dict(
91         source_type=u"host,port",
92         source_id=dict(host=host, port=port),
93         msg_type=u"ssh_result",
94         log_level=u"DEBUG",
95         timestamp=timestamp,
96         msg=u"",
97         data=dict(
98             rc=int(code),
99             stdout=str(stdout),
100             stderr=str(stderr),
101             duration=float(duration),
102         ),
103     )
104     data[u"log"].append(ssh_record)
105
106
107 def export_ssh_timeout(host, port, stdout, stderr, duration):
108     """Add a log item about ssh execution timing out.
109
110     Only for debug log.
111
112     There is no easy way to pair with the corresponding command,
113     but usually there is only one SSH session for given host and port.
114
115     Message is empty, data has fields "stdout", "stderr" and "duration".
116     The duration value may give a hint if that is not the case.
117     Log level is always DEBUG.
118
119     The host is NOT added to the info set of hosts, as each timeout
120     comes after a command.
121
122     :param host: Node "host" attribute, usually its IPv4 address.
123     :param port: SSH port number to use when connecting to the host.
124     :param stdout: Captured standard output of the command execution so far.
125     :param stderr: Captured error output of the command execution so far.
126     :param duration: How long has the command been executing, in seconds.
127     :type host: str
128     :type port: int
129     :type stdout: str
130     :type stderr: str
131     :type duration: float
132     """
133     timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
134     data = get_export_data()
135     ssh_record = dict(
136         source_type=u"host,port",
137         source_id=dict(host=host, port=port),
138         msg_type=u"ssh_timeout",
139         log_level=u"DEBUG",
140         timestamp=timestamp,
141         msg=u"",
142         data=dict(
143             stdout=str(stdout),
144             stderr=str(stderr),
145             duration=float(duration),
146         ),
147     )
148     data[u"log"].append(ssh_record)