3 # Copyright(c) 2017 Intel Corporation. All rights reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # Roman Korynkevych <romanx.korynkevych@intel.com>
25 HOSTNAME = socket.gethostname()
26 PROG_NAME = 'ovs_pmd_stats'
29 MAIN_THREAD = 'main thread'
30 PMD_THREAD = 'pmd thread'
32 REQUEST_MESSAGE = '{"id":0,"method":"dpif-netdev/pmd-stats-show","params":[]}'
33 RESPONSE_MESSAGE_TIMEOUT = 1.0
36 parser = argparse.ArgumentParser(prog=PROG_NAME)
37 parser.add_argument('--socket-pid-file', required=True, help='ovs-vswitchd.pid file location')
38 args = parser.parse_args()
41 fp = open(args.socket_pid_file, 'r')
45 logging.error('I/O error({}): {}'.format(e.errno, e.strerror))
48 logging.error('Unexpected error:', sys.exc_info()[0])
51 server_address = args.socket_pid_file.replace('.pid', '.{}.ctl'.format(pid.strip()))
53 # open unix socket to ovs-vswitch
54 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
56 sock.connect(server_address)
57 except socket.error as msg:
58 logging.error('Socket address: {} Error: {}'.format(server_address, msg))
62 sock.settimeout(RESPONSE_MESSAGE_TIMEOUT)
65 sock.sendall(REQUEST_MESSAGE)
67 # listen for respnse message
71 rdata += sock.recv(4096)
73 if rdata.count('{') == rdata.count('}'):
75 except socket.timeout:
76 logging.error('Response message has not been received in {} sec.'.format(RESPONSE_MESSAGE_TIMEOUT))
78 except socket.error as e:
79 logging.error('Error received while reading: {}'.format(e.strerror))
84 s = json.loads(rdata, strict=False)
85 except ValueError as e:
86 logging.error('Failed to parse JSON response: {}'.format(e.strerror))
89 # check for key string presence in the string
90 if 'result' not in s or 'id' not in s or 'error' not in s:
91 logging.error("One of the keys: ['id'], ['result'], ['error'] is missed in the response")
92 logging.error('Msg: {}'.format(s))
95 array = s['result'].replace('\t', '').splitlines()
97 # submit metrics in collectd format
100 if MAIN_THREAD in el or PMD_THREAD in el:
101 plugin_instance = el[:-1].replace(' ', '_')
103 type_instance = el.split(':')[0].replace(' ', "_")
104 value = el.split(':')[1].strip().split(' ')[0]
105 print('PUTVAL %s/%s-%s/%s-%s N:%s' % (HOSTNAME, PROG_NAME, plugin_instance, TYPE, type_instance, value))