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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Module with keywords that publish metric and other log events.
19 from resources.libraries.python.model.util import get_export_data
22 def export_ssh_command(host, port, command):
23 """Add a log item about SSH command execution starting.
25 The log item is present only in raw output.
26 Result arrives in a separate log item.
27 Log level is always DEBUG.
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.
32 The host is added to the info set of hosts.
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.
41 timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
42 data = get_export_data()
44 source_type=u"host,port",
45 source_id=dict(host=host, port=port),
46 msg_type=u"ssh_command",
52 data[u"hosts"].add(host)
53 data[u"log"].append(ssh_record)
56 def export_ssh_result(host, port, code, stdout, stderr, duration):
57 """Add a log item about ssh execution result.
59 Only for raw output log.
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.
65 Message is empty, data has fields "rc", "stdout", "stderr" and "duration".
66 Log level is always DEBUG.
68 The host is NOT added to the info set of hosts, as each result
69 comes after a command.
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).
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.
88 timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
89 data = get_export_data()
91 source_type=u"host,port",
92 source_id=dict(host=host, port=port),
93 msg_type=u"ssh_result",
101 duration=float(duration),
104 data[u"log"].append(ssh_record)
107 def export_ssh_timeout(host, port, stdout, stderr, duration):
108 """Add a log item about ssh execution timing out.
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.
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.
119 The host is NOT added to the info set of hosts, as each timeout
120 comes after a command.
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.
131 :type duration: float
133 timestamp = datetime.datetime.utcnow().strftime(u"%Y-%m-%dT%H:%M:%S.%fZ")
134 data = get_export_data()
136 source_type=u"host,port",
137 source_id=dict(host=host, port=port),
138 msg_type=u"ssh_timeout",
145 duration=float(duration),
148 data[u"log"].append(ssh_record)