3 ############################################################################
4 # Copyright 2016 RIFT.IO Inc #
6 # Licensed under the Apache License, Version 2.0 (the "License"); #
7 # you may not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 ############################################################################
30 def ping_rate(yaml_cfg
, logger
):
31 '''Use curl to configure ping and set the ping rate'''
33 # Get the required and optional parameters
34 params
= yaml_cfg
['parameters']
35 mgmt_ip
= params
['mgmt_ip']
37 if 'mgmt_port' in params
:
38 mgmt_port
= params
['mgmt_port']
43 cmd
= 'curl -D /dev/stdout -H "Accept: application/json" ' \
44 '-H "Content-Type: application/json" ' \
45 '-X POST -d "{{\\"rate\\":{rate}}}" ' \
46 'http://{mgmt_ip}:{mgmt_port}/api/v1/ping/rate'. \
52 logger
.debug("Executing cmd: %s", cmd
)
53 proc
= subprocess
.Popen(cmd
, shell
=True,
54 stdout
=subprocess
.PIPE
,
55 stderr
=subprocess
.PIPE
)
59 logger
.debug("Process: {}".format(proc
))
61 return proc
.returncode
64 def main(argv
=sys
.argv
[1:]):
66 parser
= argparse
.ArgumentParser()
67 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
68 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
69 args
= parser
.parse_args()
71 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
72 if not os
.path
.exists(run_dir
):
74 log_file
= "{}/ping_rate-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
76 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
77 logger
= logging
.getLogger('ping-rate')
78 logger
.setLevel(logging
.DEBUG
)
80 fh
= logging
.FileHandler(log_file
)
81 fh
.setLevel(logging
.DEBUG
)
83 ch
= logging
.StreamHandler()
85 ch
.setLevel(logging
.DEBUG
)
87 ch
.setLevel(logging
.INFO
)
89 # create formatter and add it to the handlers
90 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
91 fh
.setFormatter(formatter
)
92 ch
.setFormatter(formatter
)
96 except Exception as e
:
97 logger
.exception("Exception in {}: {}".format(__file__
, e
))
101 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
102 yaml_str
= args
.yaml_cfg_file
.read()
103 yaml_cfg
= yaml
.load(yaml_str
)
104 logger
.debug("Input YAML: {}".format(yaml_cfg
))
106 rc
= ping_rate(yaml_cfg
, logger
)
107 logger
.info("Return code: {}".format(rc
))
110 except Exception as e
:
111 logger
.exception("Exception in {}: {}".format(__file__
, e
))
114 if __name__
== "__main__":