Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Monitor dmesg and run script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
trumee
Guru
Guru


Joined: 02 Mar 2003
Posts: 547
Location: London,UK

PostPosted: Wed Sep 05, 2018 2:34 am    Post subject: Monitor dmesg and run script Reply with quote

Hello,

I would like to monitor dmesg and run a script when the following text is found,

Code:

config call request for value 0x800 returned -110


Is there a way i can do this?

Thanks
Back to top
View user's profile Send private message
russK
l33t
l33t


Joined: 27 Jun 2006
Posts: 607

PostPosted: Wed Sep 05, 2018 4:17 am    Post subject: Reply with quote

trumee,

If you want to roll your own, here is a very crude start:
Code:
#!/bin/bash

# change MYSCRIPT to do what you want
MYSCRIPT="/bin/date"

if [ \! -x $MYSCRIPT ]
then
    echo script $MYSCRIPT not found or not executable
    exit 1
fi

dmesg -w | while read m
do
    echo $m | grep -e 'config call request for value 0x800 returned -110' >/dev/null && $MYSCRIPT
done


It may have some undesirable features, like when you start this script, it will react to old messages already in your dmesg.

Other things you might think about, search for log monitoring tools like 'logwatch', these could be cajoled into doing what you want.

Also if you have systemd, there may be other options like journalctl tricks, but I am no expert.

HTH
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6229
Location: Room 101

PostPosted: Wed Sep 05, 2018 3:37 pm    Post subject: Reply with quote

russK wrote:
It may have some undesirable features, like when you start this script, it will react to old messages already in your dmesg.

russK ... that can most likely be resolved by piping the output of dmesg to 'tail --lines=1 --follow', and it might also be a good idea to have '--level', and '--facility', as parameters to dmesg (so as to reduce the output to only the level/facility we're expecting to contain the string). Untested but I'm expecting the following to work:

Code:
dmesg --level=err --facility=kern --notime --follow | tail --lines=1 --follow | egrep --quiet '0x800.returned.-110' && script.sh

That said, logwatch or similar is probably the far better option here.

best ... khay
Back to top
View user's profile Send private message
trumee
Guru
Guru


Joined: 02 Mar 2003
Posts: 547
Location: London,UK

PostPosted: Wed Sep 05, 2018 11:54 pm    Post subject: Reply with quote

russK wrote:
trumee,

It may have some undesirable features, like when you start this script, it will react to old messages already in your dmesg.

Other things you might think about, search for log monitoring tools like 'logwatch', these could be cajoled into doing what you want.

Also if you have systemd, there may be other options like journalctl tricks, but I am no expert.

HTH


Thanks for the script. As it stands this script would exit once the error is detected. I need something which can run in the background, and keep on monitoring dmesg.

I installed logwatch, but dont see a service script in /etc/init.d and suspect it gets triggered with cron. If that is so, it wont be of use since I need something to monitor /var/log/messages or dmesg on the fly.

I am still getting my head around this but wuseman on irc set me up the following script which runs in the background,

Code:

#!/bin/bash
trap '~/cycle.sh ' EXIT

n=$(dmesg | grep -o  'config call request for value 0x800 returned -110' | wc -l)
while [ "$n" -eq "$(dmesg | grep -o  'config call request for value 0x800 returned -110' | wc -l)" ]
do
    sleep 0.1s
done
sleep 0.1s
#dmesg | grep -o 'config call request for value 0x800 returned -110' | tail -n+$n | sort -u
echo "Recorder has crashed, restarting.."
# <remove comment and put command for restart recorder here
Back to top
View user's profile Send private message
russK
l33t
l33t


Joined: 27 Jun 2006
Posts: 607

PostPosted: Thu Sep 06, 2018 2:33 am    Post subject: Reply with quote

khayyam wrote:
russK ... that can most likely be resolved by piping the output of dmesg to 'tail --lines=1 --follow'

khay, I tried the suggestion and it had some kind of error that wasn't immediately obvious to me, it might have something to do with the combination of the --lines and --follow options.

trumee wrote:
Thanks for the script. As it stands this script would exit once the error is detected. I need something which can run in the background, and keep on monitoring dmesg.

trumee, What made you conclude the script would exit? The '-w' arg to dmesg makes it follow the kernel ring buffer. When I tested the script, it did not exit. Note, you can test the script with a command like this:
Code:
echo config call request for value 0x800 returned -110 | tee /dev/kmsg


There may be a reliability issue the script from IRC, because as your kernel ringbuffer fills, the count of lines having the trigger string could decrease or go to 0, triggering the call to the other script. Also it kind-of needlessly burns CPU cycles. But if it suits your needs anyway, no worries.

Regards
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6229
Location: Room 101

PostPosted: Thu Sep 06, 2018 11:38 am    Post subject: Reply with quote

trumee wrote:
I am still getting my head around this but wuseman on irc set me up the following script which runs in the background,

Code:
n=$(dmesg | grep -o  'config call request for value 0x800 returned -110' | wc -l)
while [ "$n" -eq "$(dmesg | grep -o  'config call request for value 0x800 returned -110' | wc -l)" ]
do
    sleep 0.1s
done
sleep 0.1s

trumee ... this is just silly, commands return exit status, the numerical equation, and command substitution to get the value, is simply pointless:

Code:
% for i in foo bar baz ; do echo $i | grep -q 'baz' ; if [[ $? == "0" ]]; then echo "we have a match: the match is $i" ; fi ; done
we have a match: the match is baz

or (similarly) we can simply execute on success:

Code:
% for i in foo bar baz ; do echo $i | grep -q 'bar' && echo "we have a match: the match is $i" ; done
we have a match: the match is bar

Also, the above script from wuseman will grep dmesg every 0.1 seconds, when really all you need do is follow some output (probably /var/log/messages is the better option). So, putting all that together:

Code:
#!/bin/bash

while read ; do
    egrep -q '0x800.returned.-110' && your_script/command
done < <(tail -f -n 1 /var/log/messages)

That will continue to parse /var/log/messages, and for each match of '0x800.returned.-110', run the "command" (your "restart recorder" script, or what-have-you). More importantly it won't be running dmesg|grep ten times a second :)

khayyam wrote:
russK ... that can most likely be resolved by piping the output of dmesg to 'tail --lines=1 --follow'

russK wrote:
khay, I tried the suggestion and it had some kind of error that wasn't immediately obvious to me, it might have something to do with the combination of the --lines and --follow options.

russK ... yeah, not sure what's happening there but if you pipe 'dmesg --follow | tail --follow' nothing comes out the pipe :P

best ... khay
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum