How do I use Python file in Nagios? - python

I'm trying to get a response from Nagios by using the following Python code and instructions:
http://skipperkongen.dk/2011/12/06/hello-world-plugin-for-nagios-in-python/
From some reason I never get to have OK from Nagios and it's always comes back with the message: Return code 126 is out of bounds - plugin may be missing
I installed nagiosplugin 1.0.0, and still nothing seems to be working
In parallel I have some other services (not python files) that work e.g. http check, current users, and SSH
What am I doing wrong? I'm trying to solve that for few days already

Getting Nagios to utilize your new plug-in is quite easy. You should make changes to three files and restart Nagios — that’s all it takes.
The first file is /etc/nagios/command-plugins.cfg (leave comment please if you know path to this file or analog in ubuntu). Assumed that plugin file is placed in /usr/lib/nagios/plugins/ directory:
command[check_hello_world]=/usr/lib/nagios/plugins/check_helloworld.py -m 'some message'
Drop down one directory to /etc/nagios/objects/commands.cfg (for ubuntu user should create cfg file in that dir /etc/nagios-plugins/config/):
define command {
command_name check_hello_world
command_line $USER1$/check_hello_world.py -m 'some message'
}
Save the file and open up /etc/nagios/objects/localhost.cfg (in ubuntu path to service definition files located in /etc/nagios3/nagios.cfg and by default cfg_dir=/etc/nagios3/conf.d. So, to define new service in ubuntu user should create cfg file in that dir, for example hello.cfg). Locate this section:
#
# SERVICE DEFINITIONS
#
and add new entry:
define service {
use local-service ; Name of service template to use
host_name localhost
service_description Check using the hello world plugin (always returns OK)
check_command check_hello_world
}
All that remains is to restart Nagios and to verify that plug-in is working. Restart Nagios by issuing the following command:
/etc/init.d/nagios restart
http://www.linux-mag.com/id/7706/
ubuntuforums.org - Thread: My Notes for Installing Nagios on Ubuntu Server 12.04 LTS

I had to prepend the path to python2.7 even though the shebang in the file specified it.
In the command definition I had this:
command_line /usr/local/bin/python2.7 $USER1$/check_rabbit_queues.py --host $HOSTADDRESS$ --password $ARG1$
Even though the top of the actual python file had:
#!/usr/bin/env python2.7
Even though the script executed and returned just fine from the command line without specifying the interpreter.
Nothing else I tried seemed to work.

Related

Getting error 'Browsermob-Proxy binary couldn't be found in path provided' even after installing required proxy

I am using a Mac and installed the required files from bmp.lightbody.net, and unzipped them. I tried creating a Server in my Python file using the following code.
server = Server('/Users/username/Downloads/⁨browsermob-proxy-2.1.4⁩/bin⁩/browsermob-proxy')
I was met with the following error
browsermobproxy.exceptions.ProxyServerError: Browsermob-Proxy binary couldn't be found in path provided: /Users/username/Downloads/⁨browsermob-proxy-2.1.4⁩/bin⁩/browsermob-proxy
The path runs perfectly in my Terminal, and I was able to create a proxy.
curl -X POST localhost:8080/proxy
{"port":8081}
I have also tried appending the folder to my path using sys and moving the browsermob folder to the folder where the python file is present, but nothing seems to work.
Any help would be welcome. Thanks!
Similar question
Try to add the actual directory to the PATH
echo 'export PATH=$PATH:/Users/username/Downloads/⁨browsermob-proxy-2.1.4⁩/bin⁩' >> ~/.bashrc
This way you don't have to specify the path arg on the Server instance manually
from browsermobproxy import Server
dict = {'port': 8090}
server = Server(options=dict)

python don't extract zipfile when ran through crontab

#!/usr/bin/python
import requests, zipfile, StringIO, sys
extractDir = "myfolder"
zip_file_url = "download url"
response = requests.get(zip_file_url)
zipDocument = zipfile.ZipFile(StringIO.StringIO(response.content))
zipinfos = zipDocument.infolist()
for zipinfo in zipinfos:
extrat = zipDocument.extract(zipinfo,path=extractDir)
System configuration
Ubuntu OS 16.04
Python 2.7.12
$ python extract.py
when I run the code on Terminal with above command, it works properly and create the folder and extract the file into it.
Similarly, when I create a cron job using sodu rights the code executes but don't create any folder or extracts the files.
crontab command:-
40 10 * * * /usr/bin/sudo /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
also tried
40 10 * * * /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
Notes :
I check the syslog, it says the cron is running successfully
The above code gives no errors
also made the python program executable by chmod +x filename.py
Please help where am I going wrong.
Oups, there is nothing really wrong in running a Python script in crontab, but many bad things can happen because the environment is not the one you are used to.
When you type in an interactive shell python directory.py, the PATH and all required PYTHON environment variable have been set as part of login and interactive shell initialization, and the current directory is your home directory by default or anywhere you currently are.
When the same command is run from crontab, the current directory is not specified (but may not be what you expect), PATH is only /bin:/usr/bin and python environment variables are not set. That means that you will have to tweak environment variables in crontab file until you get a correct Python environment, and set the current directory.
I had a very similar problem and it turned out cron didn’t like importing matplotlib, I ended up having to specify Agg backend. I figured it out by putting log statements after each line to see how far the program got before it crapped out. Of course, my log was empty which tipped me off that it crashed on imports.
TLDR: log each line inside the script

VATIC Annotation Tool installation - "turkic setup --database" - unknown action setup

I'm trying to install VATIC Video Annotation Tool on Linux. I followed the instructions in README file twice, always failing to execute command:
$ turkic setup --database
which gives these two error messages:
No handlers could be found for logger "turkic.geolocation"
Error: Unknown action setup
Other turkic commands, e.g. turkic status --verify give the same error messages (for a given action name).
I also noticed that source file ~/vatic/public/index.html contains links to stylesheets and scripts in turkic folder src="/turkic/file_name", which can't be reached. Their true location is in ~/turkic/turkic/public.
Any ideas what can be wrong?
You should go into vatic folder when executing any commands starting with turkic.
Only inside vatic folder "actions" will be recognized.
Make sure that you issue the symbolic link command:
$ turkic setup --public-symlink

directory size of a linux machine using python fabric

I am trying to print the directory size using python fabric. I used the below code
def getfilesize():
with settings(user='hduser',password='cisco'):
path='/app/hadoop/tmp/myoutput/'
os.path.getsize(path)
but it throws me an error " no such file or directory"
But I can see this directory
hduser#dn1:~$ cd /app/hadoop/tmp/myoutput/
hduser#dn1:/app/hadoop/tmp/myoutput$ ls
taskTracker tt_log_tmp ttprivate userlogs
Am i doing any sytax error here ?
This isn't a syntax error, rather the python code is still being executed on your machine and not the remote machine. So calling os.path.getsize is checking the path size on your local machine (where it does not exist).
Instead you need to use fabric to execute shell commands on the remote server and catch their output. There are fabric modules that wrap common use cases, like the files module, so you don't have to work in terms of bash commands. Unfortunately I don't know of any that will give you a recursive directory size. Fortunately, getting the size of a directory is just a oneliner so we can do something like:
def getfilesize():
with settings(user='hduser', password='cisco'):
output = run('du -s "/app/hadoop/tmp/myoutput/"')
# output is the commands stdout as a (potentially multiline) string
# for `du` it will look like: "183488582 /app/hadoop/tmp/mypoutput"
size_in_bytes = int(output.split()[0])

Django/Python convert PDF using shell (os.system/Popen) not working in production

I'm using libreoffice on CLI to convert some documents to PDFs within Django.
This is in my view (and also tried in save method on model):
outdir = '/'.join([settings.MEDIA_ROOT, 'pdf'])
command = ['libreoffice', '--headless', '--convert-to', 'pdf', '-outdir', outdir, form.instance.upload.path]
stdout, stderr = subprocess.Popen(command).communicate()
I've also tried the following in place of subprocess.Popen:
os.system("libreoffice --headless --convert-to pdf --outdir %s %s" % (outdir, instance.upload.path))
Then I check it out:
# quick check new file exists
with open("%s/%s.pdf" % (outdir, os.path.splitext(instance.upload.name)[0])): pass
When I run them through the website is just doesn't work -- the PDF file isn't generated.
Thing is: both these methods work both in my local environment and when the functions that they're in are called from $ python manage.py shell
The problem is only in production.
Django 1.4
Python 2.7.3
Server version: Apache/2.2.22 (Ubuntu) Server built: Feb 13 2012 01:51:50 (Rackspace)
Note that I thought I might be experiencing this bug: Popen does not work anymore with apache/wsgi and python 2.7.2? ...
Though Graham's work around as follows (I added this to my apache2.conf ) doesn't seem to change anything.
WSGIApplicationGroup %{GLOBAL}
My sys admin skills aren't great though and I may have done this wrong, and I'm totally stumped, any advice or directions of other things to try or work-arounds would be appreciated.
mod_wsgi when spawned from the apache2 process won't have the $PATH defined that you have on your shell, so you should pass subprocess.Popen the absolute pathname for the libreoffice binary, i.e. /usr/bin/libreoffice, not just its filename.
You could import the LibreOffice / OpenOffice uno module to Python and and avoid using the command line. I've been writing up some more complete documentation, which you can download at http://documenthacker.wordpress.com/ (or maybe soon www.documenthacker.com).
There is an example in the cookbook section for converting a document to PDF.

Categories