RESTlos
RESTlos copied to clipboard
API crushes on control?restart
Trying to restart nagios from api by sending commands as you showed in examples and api crusshes.
Here is the request
curl -X POST -H "content-type: application/json" -v 'http://admin:[email protected]:5000/control?restart' * About to connect() to monitor.lanbilling.ru port 5000 (#0) * Trying 192.168.x.1... Connection refused * couldn't connect to host * Closing connection #0 curl: (7) couldn't connect to host [andruss@example ~]# curl -X POST -H "content-type: application/json" -v 'http://admin:[email protected]:5000/control?restart' * About to connect() to monitor.example.com port 5000 (#0) * Trying 192.168.x.1... connected * Connected to monitor.lanbilling.ru (192.168.x.1) port 5000 (#0) * Server auth using Basic with user 'admin' > POST /control?restart HTTP/1.1 > Authorization: Basic YWRtaW46cGFzc3dvcmQ= > User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: andruss.example.net:5000 > Accept: */* > content-type: application/json > * Empty reply from server
and api itself running everything is ok
[root@dhcp restlos]# python2.7 /opt/restlos/restlosapi.py 5000 * starting restlos V0.3 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
but when above curl command is sent got the following:
Traceback (most recent call last): File "/opt/restlos/restlosapi.py", line 363, inapp.run(port=config['port'],host='0.0.0.0') File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 843, in run run_simple(host, port, self, **options) File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 694, in run_simple inner() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 659, in inner srv.serve_forever() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 499, in serve_forever HTTPServer.serve_forever(self) File "/usr/local/lib/python2.7/SocketServer.py", line 238, in serve_forever self._handle_request_noblock() File "/usr/local/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock self.handle_error(request, client_address) File "/usr/local/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock self.process_request(request, client_address) File "/usr/local/lib/python2.7/SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "/usr/local/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/local/lib/python2.7/SocketServer.py", line 651, in __init__ self.handle() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 216, in handle rv = BaseHTTPRequestHandler.handle(self) File "/usr/local/lib/python2.7/BaseHTTPServer.py", line 340, in handle self.handle_one_request() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 251, in handle_one_request return self.run_wsgi() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 193, in run_wsgi execute(self.server.app) File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 181, in execute application_iter = app(environ, start_response) File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2000, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1559, in handle_exception handler = self._find_error_handler(InternalServerError()) File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1476, in _find_error_handler .get(code)) File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1465, in find_handler handler = handler_map.get(cls) AttributeError: 'function' object has no attribute 'get'
Hi @andrusstrockiy
that somehow looks to me like the bug encountered here #5 Do you have the latest code? Can you just try a clean clone and test it again?
Regards, Christian
Do you have the latest code? Can you just try a clean clone and test it again?
After pulling latest from master
[andruss@mon restlos]# git pull Updating 248f275..bfd927f Fast-forward contrib/uwsgi.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ restlosapi.py | 54 ++++++++++++++++++++++++++----------- utils/utils.py | 1 + 3 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 contrib/uwsgi.md
restapi stoped crushed but returned me an error 500. Here is the request.
[andruss@monitor ~]# curl -X POST -H "content-type: application/json" 'http://admin:[email protected]:5000/control?restart' { "code": 500, "message": "500: Internal Server Error" } [andruss@monitor ~]#
Application log output (added output of config.json at the start of app)
[andruss@mon examples]# python2.7 /opt/restlos/restlosapi.py * starting restlos V0.3 Config {'logging': {'handlers': {'syslog': {'facility': u'daemon', 'formatter': u'syslog', 'class': u'logging.handlers.SysLogHandler', 'address': u'/dev/log', 'level': u'WARN'}, 'console': {'class': u'logging.StreamHandler', 'level': u'DEBUG'}}, 'version': 1, 'root': {'level': u'DEBUG', 'propagate': True, 'handlers': [u'console', u'syslog']}, 'formatters': {'syslog': {'class': u'logging.Formatter', 'format': u'nagios-api[%(process)d]: %(message)s'}}}, 'nagios_main_cfg': u'/etc/nagios/nagios.cfg', 'sudo': False, 'nagios_bin': u'/usr/sbin/nagios', 'auth': {'params': {}, 'provider': u'AuthDict'}, 'host': u'0.0.0.0', 'output_dir': u'/etc/nagios/objects/api', 'port': 5000} * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 192.168.x.1 - - [26/Aug/2016 10:28:15] "POST /control?verify HTTP/1.1" 500 - 192.168.x.1 - - [26/Aug/2016 10:30:52] "POST /control?restart HTTP/1.1" 500 - 192.168.x.1 - - [26/Aug/2016 10:31:55] "POST /control?restart HTTP/1.1" 500 -
Can you change this list: https://github.com/Crapworks/RESTlos/blob/master/restlosapi.py#L385
to:
app.run(host=config['host'], port=config['port'], debug=True)
and try again? Hopefully this will give us some more informations.
Here is the output
[andruss@mon ~]# python2.7 /opt/restlos/restlosapi.py /opt/restlos * starting restlos V0.3 Config {'logging': {'handlers': {'syslog': {'facility': u'daemon', 'formatter': u'syslog', 'class': u'logging.handlers.SysLogHandler', 'address': u'/dev/log', 'level': u'DEBUG'}, 'console': {'class': u'logging.StreamHandler', 'level': u'DEBUG'}}, 'version': 1, 'root': {'level': u'DEBUG', 'propagate': True, 'handlers': [u'console', u'syslog']}, 'formatters': {'syslog': {'class': u'logging.Formatter', 'format': u'nagios-api[%(process)d]: %(message)s'}}}, 'nagios_main_cfg': u'/etc/nagios/nagios.cfg', 'sudo': False, 'nagios_bin': u'/usr/sbin/nagios', 'auth': {'params': {}, 'provider': u'AuthDict'}, 'host': u'0.0.0.0', 'output_dir': u'/etc/nagios/objects/api', 'port': 5000} * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat /opt/restlos * starting restlos V0.3 Config {'logging': {'handlers': {'syslog': {'facility': u'daemon', 'formatter': u'syslog', 'class': u'logging.handlers.SysLogHandler', 'address': u'/dev/log', 'level': u'DEBUG'}, 'console': {'class': u'logging.StreamHandler', 'level': u'DEBUG'}}, 'version': 1, 'root': {'level': u'DEBUG', 'propagate': True, 'handlers': [u'console', u'syslog']}, 'formatters': {'syslog': {'class': u'logging.Formatter', 'format': u'nagios-api[%(process)d]: %(message)s'}}}, 'nagios_main_cfg': u'/etc/nagios/nagios.cfg', 'sudo': False, 'nagios_bin': u'/usr/sbin/nagios', 'auth': {'params': {}, 'provider': u'AuthDict'}, 'host': u'0.0.0.0', 'output_dir': u'/etc/nagios/objects/api', 'port': 5000} * Debugger is active! * Debugger pin code: 107-397-477 192.168.x.1 - - [29/Aug/2016 17:14:02] "POST /control?verify HTTP/1.1" 500 - 192.168.x.1 - - [29/Aug/2016 17:14:26] "POST /control?restart HTTP/1.1" 500 -
so far no output which could lead to clue.
Curious which version of pynag you used in development? It would be nice if you provided a requirements.txt file. When I run the pynag (pynag-0.9.1) call that NagiosControlView._restart makes I get AttributeError.
NagiosControlView._restart https://github.com/Crapworks/RESTlos/blob/master/restlosapi.py#L153
>>> from pynag import Model
>>> Model.Control.Command.restart_program(command_file="/usr/local/nagios/etc/nagios.cfg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Control'
Looks like pynag made some changes to the API. Below seems to work.
>>> from pynag.Control import Command
>>> Command.restart_program(command_file="/usr/local/nagios/etc/nagios.cfg")