So, you want to log the errors AND have them show up in the output, put
your logic into a script:

#!/bin/env bash
rval=$(/usr/bin/python /home/user/test.py >> /home/user/test.log 2>&1)
if [[ $rval ~= 0 ]]; then
   tail -n 10 /home/user/test.log
   exit $rval
fi
exit 0

So, in the script above, we attempt to run the python script and check the
return value. If it's non zero, write the last 10 lines of test.log to
stdout and exit with the non-zero exit code.

If you're better with python, you could also open a single file for stdout
and stderr e.g.

import sys
f = open('/home/user/test.log', 'w')
o = sys.stdout
sys.stdout = f
sys.stderr = f

# do some work using python and check if
# something bad happened, when that happens we want to log it

message = "Something bad happened"
print message

# switch stdout back, write message to stdout and exit
sys.stdout = o
print message
exit(1)
# EOF

Russ

On Mon, Feb 8, 2016 at 4:59 PM, Vikram Kone <[email protected]> wrote:

> I tried this, and it now logs the stdout and stderr correctly in the log
> file. But the problem is, the output from python script is no longer being
> sent in the alert email body
>
> check program mytest with path "/bin/bash -c '/usr/bin/python
> /home/user/test.py >> /home/user/test.log 2>&1'"
>       if status != 0 then alert
>
> The alert email just says "'/bin/bash' failed with exit status (1) -- no
> output"
>
> I want to see the python script stderr message here ie "Value Error ..".
>
> How do I do this?
>
> On Mon, Feb 8, 2016 at 1:18 PM, Russell Simpkins <
> [email protected]> wrote:
>
>> Try creating a simple bash script and then make the check run your bash
>> script
>>
>> check program my_program with path "/usr/local/bin/python_check.sh" ....
>>
>> That way you can make sure that it's not the monit check creating issues.
>>
>> ----
>> #!/bin/bash
>> /usr/bin/python -u user my_program.py > /var/log/mylogs 2>&1
>> ----
>>
>> You can then execute the bash script to verify your logs are getting
>> written.
>>
>> On Mon, Feb 8, 2016 at 4:10 PM, Vikram Kone <[email protected]> wrote:
>>
>>> yes, it works when I run the python command directly from shell.
>>> but when I do
>>>
>>> $> monit start my_program
>>>
>>> I don't see the log file being created
>>>
>>> On Mon, Feb 8, 2016 at 12:22 PM, Russell Simpkins <
>>> [email protected]> wrote:
>>>
>>>> I created test.py
>>>>
>>>> raise ValueError("throwing up")
>>>>
>>>> I ran it by running
>>>>
>>>> $> python test.py > /dev/null 2>&1
>>>> $> echo $?
>>>> 1
>>>>
>>>> Not sure what else you might be doing wrong
>>>>
>>>>
>>>> On Mon, Feb 8, 2016 at 3:13 PM, Vikram Kone <[email protected]>
>>>> wrote:
>>>>
>>>>> For testing it I simply did this in the python script
>>>>>
>>>>> #my_program.py
>>>>> #!/usr/bin/python
>>>>>
>>>>> raise ValueError("throwing up")
>>>>>
>>>>>
>>>>> On Mon, Feb 8, 2016 at 10:39 AM, Russell Simpkins <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> Virkam,
>>>>>>
>>>>>> You don't show your python. Are you using "exit(1)" in your python
>>>>>> when there is an error condition?
>>>>>> You need to exit(1) or something other than zero.
>>>>>>
>>>>>> Russ
>>>>>>
>>>>>> On Mon, Feb 8, 2016 at 1:06 PM, Vikram Kone <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>> I have a python script that I use as to check certain system
>>>>>>> conditions to alert on using monit.
>>>>>>> This script prints a buch of stuff during execution to stdout which
>>>>>>> I want to capture in a log file.
>>>>>>> How should I configure the monit conf script, such that I can
>>>>>>> capture both the stdout and stderr of this script and at the same time
>>>>>>> alerting on the exit status of the script. The monit alert should also
>>>>>>> included the stdout/stderr for the alert events.
>>>>>>>
>>>>>>> This is what I tried
>>>>>>>
>>>>>>> #/etc/monit/conf/myprogram.conf
>>>>>>>
>>>>>>> check program my_program with path "/usr/bin/python -u
>>>>>>> /opt/program/my_program.py > my_prgoram.log 2&>1"
>>>>>>>  if status !=0 alert
>>>>>>>
>>>>>>> But I see that the monit always thinks that the program is reporting
>>>>>>> status=0 even when then it exists with error code 1.
>>>>>>>
>>>>>>> What am i doing wrong?
>>>>>>>
>>>>>>> --
>>>>>>> To unsubscribe:
>>>>>>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> To unsubscribe:
>>>>>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> To unsubscribe:
>>>>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>>>>
>>>>
>>>>
>>>> --
>>>> To unsubscribe:
>>>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>>>
>>>
>>>
>>> --
>>> To unsubscribe:
>>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>>
>>
>>
>> --
>> To unsubscribe:
>> https://lists.nongnu.org/mailman/listinfo/monit-general
>>
>
>
> --
> To unsubscribe:
> https://lists.nongnu.org/mailman/listinfo/monit-general
>
--
To unsubscribe:
https://lists.nongnu.org/mailman/listinfo/monit-general

Reply via email to