Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
TIP: Send SMS alerts when emerge completes
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2671
Location: here, there or in transit

PostPosted: Fri Feb 27, 2009 12:27 am    Post subject: TIP: Send SMS alerts when emerge completes Reply with quote

My most recent large update (>500 packages) prompted this idea. Around the tenth time an ebuild failed mere moments after I left the room (not to return for hours), I decided to have the darn thing alert me when it wanted attention.

The following assumes you have installed:
  • The mailx client (mail-client/mailx), to send mail from shell scripts and the command line.
  • An MTA (mail transfer agent) for mailx to use, such as Postfix, Sendmail or ssmtp.

This script is intended to run in a pipeline after emerge and send a short message to explain why emerge stopped.
/usr/local/sbin/emobalert:
#!/bin/sh
# Record exit status of emerge--must precede ALL other commands!
[ $? -eq 0 ] && RESULT="PASS" || RESULT="FAIL"

# Change next line to your mobile/SMS address
SMS_ADDR="2015551212@sms.some-mobile.com"

LAST_PKG="$(tac /var/log/emerge.log | awk '$2 == ">>>" {print $4,$5,$6,$7; exit;}')"
CFG_MODS="$(find /etc -iname '._cfg????_*' | wc -l)"

mail -s"Emerge Completed" $SMS_ADDR <<-EOM
   ${RESULT} ${LAST_PKG}
   :::
   ${CFG_MODS} config updates pending
   $*
EOM
Change SMS_ADDR to your phone's SMS address and be sure to mark the script executable. You might also want to make it readable only by root if you need to guard your mobile number.

Usage is simple:
Example:
emerge -uDv world; emobalert
That's it. Notice you run the script unconditionally after emerge so it can detect the exit status (that is, use the semicolon, never && or || or any other pipe connector).

When emerge exits, you'll receive a text message similar to this:
Sample message:
From: root@localhost.localdomain
(Emerge Completed)
PASS (2 of 2) app-crypt/seahorse-2.22.3
:::
0 config updates pending
The message body reports the last ebuild attempted, whether the overall merge succeeded or failed and whether you need to apply any configuration file updates. Any command-line arguments supplied to emobalert are appended to the message.

On those rare occasions when you have better things to do than watch compiler output scroll by, this trick helps you still catch problems quickly.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1553
Location: U.S.A.

PostPosted: Fri Feb 27, 2009 8:58 am    Post subject: Reply with quote

Very nice. Thank you for sharing it.

I had thought about dropping a .forward file with my sms address during emerges, but even with "email_summary", it's far too much data. This captures the important tidbits.
Back to top
View user's profile Send private message
YourNameInHere
n00b
n00b


Joined: 28 Mar 2007
Posts: 5

PostPosted: Fri Feb 27, 2009 10:19 am    Post subject: Reply with quote

what sms gateway do you use? Is there something for free to use?

thx

YourNameInHere
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1553
Location: U.S.A.

PostPosted: Fri Feb 27, 2009 10:47 am    Post subject: Reply with quote

my cell phone provider has a gateway
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2671
Location: here, there or in transit

PostPosted: Fri Feb 27, 2009 5:16 pm    Post subject: Reply with quote

BoneKracker wrote:
my cell phone provider has a gateway
Likewise.

For US Verizon Wireless numbers the gateway address is number@vtext.com for text, number@vzwpix.com for multimedia messages. I think most carriers provide a similar service (in the US at least). Check the support section of the carrier's website or call customer service.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
comprookie2000
Developer
Developer


Joined: 25 Jul 2004
Posts: 892
Location: Pompano Beach, Florida

PostPosted: Fri Mar 13, 2009 11:32 pm    Post subject: Reply with quote

Thanks works great!
I found my cell phone providers number here;
http://en.wikipedia.org/wiki/SMS_gateway#Carrier-provided_SMS_to_email_gateways
_________________
http://dev.gentoo.org/~dabbott/
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1553
Location: U.S.A.

PostPosted: Fri Mar 13, 2009 11:55 pm    Post subject: Reply with quote

comprookie2000 wrote:
Thanks works great!
I found my cell phone providers number here;
http://en.wikipedia.org/wiki/SMS_gateway#Carrier-provided_SMS_to_email_gateways

You might want to only send a few until you get a bill, so you can make sure they're not charging you (or see how much they're charging you).
Back to top
View user's profile Send private message
milomak
Apprentice
Apprentice


Joined: 10 Apr 2008
Posts: 255

PostPosted: Mon Mar 16, 2009 5:34 pm    Post subject: Reply with quote

This is very interesting the same has happened to me very time so I will be interested to see if I can implement it.
Back to top
View user's profile Send private message
.yankee
Apprentice
Apprentice


Joined: 24 Feb 2008
Posts: 194
Location: Polska

PostPosted: Mon Mar 23, 2009 12:18 am    Post subject: Reply with quote

Nice trick... But isn't it easier to use --keep-going emerge parameter?
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1553
Location: U.S.A.

PostPosted: Mon Mar 23, 2009 12:24 am    Post subject: Reply with quote

.yankee wrote:
Nice trick... But isn't it easier to use --keep-going emerge parameter?

Yeah. Or you could just use Ubuntu. :P
Back to top
View user's profile Send private message
coolsnowmen
Veteran
Veteran


Joined: 30 Jun 2004
Posts: 1479
Location: No.VA

PostPosted: Mon Mar 23, 2009 5:41 pm    Post subject: Reply with quote

BoneKracker wrote:
.yankee wrote:
Nice trick... But isn't it easier to use --keep-going emerge parameter?

Yeah. Or you could just use Ubuntu. :P

You do know you are on forums.gentoo.org right?
_________________
emerge: there are no ebuilds to satisfy "moo"
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2671
Location: here, there or in transit

PostPosted: Mon Mar 23, 2009 5:45 pm    Post subject: Reply with quote

.yankee wrote:
Nice trick... But isn't it easier to use --keep-going emerge parameter?
The two are not mutually exclusive.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1553
Location: U.S.A.

PostPosted: Mon Mar 23, 2009 6:59 pm    Post subject: Reply with quote

coolsnowmen wrote:
BoneKracker wrote:
.yankee wrote:
Nice trick... But isn't it easier to use --keep-going emerge parameter?

Yeah. Or you could just use Ubuntu. :P

You do know you are on forums.gentoo.org right?

You do know that was joke, right?
Back to top
View user's profile Send private message
polovinamozga
n00b
n00b


Joined: 11 Sep 2008
Posts: 13

PostPosted: Wed Mar 25, 2009 4:48 am    Post subject: Reply with quote

great script!
Back to top
View user's profile Send private message
.yankee
Apprentice
Apprentice


Joined: 24 Feb 2008
Posts: 194
Location: Polska

PostPosted: Sun Mar 29, 2009 1:59 pm    Post subject: Reply with quote

timeBandit wrote:
.yankee wrote:
Nice trick... But isn't it easier to use --keep-going emerge parameter?
The two are not mutually exclusive.


True... Though, if I understand the script well, it seems that the informative value of running it after emerge -keep-going would be very small. Did you think about modifying it so that it would parse the log more thoroughly? I guess it wouldn't be that difficult to detect, how many packages were skipped during such emerge job.
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2671
Location: here, there or in transit

PostPosted: Wed Apr 08, 2009 2:21 pm    Post subject: Reply with quote

.yankee wrote:
True... Though, if I understand the script well, it seems that the informative value of running it after emerge -keep-going would be very small. Did you think about modifying it so that it would parse the log more thoroughly? I guess it wouldn't be that difficult to detect, how many packages were skipped during such emerge job.
Agreed, but I have no need. I am not too fond of --keep-going, I prefer to fix problems as they arise (as you might have guessed :wink:). When I have some time I'll look into counting failed packages. I have an idea for a simple solution but need to experiment.

Thanks for all the compliments and comments so far. :)
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
BonezTheGoon
Bodhisattva
Bodhisattva


Joined: 14 Jun 2002
Posts: 1375
Location: Albuquerque, NM -- birthplace of Microsoft and Gentoo

PostPosted: Tue Feb 16, 2010 4:46 pm    Post subject: Reply with quote

This is one of the coolest things I have ever seen someone contribute! I really would like to see this suggested as a feature update to portage so that you can just define an address in your /etc/make.conf and have it done every time without remembering to run the script. Have you submitted it as a feature request? I'd certainly vote for it's injection!!

Awesome work! Thanks so much for sharing!!
_________________
mcgruff wrote:
I can't promise to be civil.


pjp wrote:
The greater evil is voting for the "lesser evil."
Back to top
View user's profile Send private message
bobspencer123
Guru
Guru


Joined: 19 Dec 2005
Posts: 544

PostPosted: Tue Feb 16, 2010 4:55 pm    Post subject: Reply with quote

Hey this looks like a great little script. Thanks so much for your effort.

I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.
_________________
Increasingly becoming a 2 channel audio nut!
Back to top
View user's profile Send private message
lazy_bum
l33t
l33t


Joined: 16 Feb 2005
Posts: 689

PostPosted: Wed Feb 17, 2010 10:04 pm    Post subject: Reply with quote

bobspencer123 wrote:
Hey this looks like a great little script. Thanks so much for your effort.

I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.

Sure you can. I've made some quick and ugly (and it is *ugly*) hack some days ago to send also latest logfile path (this could be edited to send ie. tail -100 failed.log).
Code:
#!/bin/sh
# Record exit status of emerge--must precede ALL other commands!
[ $? -eq 0 ] && RESULT="PASS" || RESULT="FAIL"

# Change next line to your mobile/SMS address
SMS_ADDR="your@mail"

LAST_PKG="$(tac /var/log/emerge.log | awk '$2 == ">>>" {print $4,$5,$6,$7; exit;}')"
CFG_MODS="$(find /etc -iname '._cfg????_*' | wc -l)"
LOG_PKG="$(ls -l -rt /var/log/portage/ | grep -v elog | tail -1 | awk '{print $9}')"

mail -s"Emerge Completed" $SMS_ADDR <<-EOM
   ${RESULT} ${LAST_PKG}
   :::
   see latest logfile /var/log/portage/${LOG_PKG}
   $*
EOM

_________________
roslin uberlay | grubelek
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2671
Location: here, there or in transit

PostPosted: Thu Feb 25, 2010 2:19 pm    Post subject: Reply with quote

BonezTheGoon wrote:
Awesome work! Thanks so much for sharing!!
:D Thanks! Sorry for not responding sooner, this thread fell off my egosearch due to age.

bobspencer123 wrote:
I was wondering if this could be edited to include the ability to send an email...
Er...that's what it does. :wink: Conversion to SMS happens at the carrier's gateway address, not in the script. Just change SMS_ADDR to the email address where you want to send the note.

Note: The script is partially broken as of Portage 2.1.6.13 and later (at least in the 2.1.x series). The exit status of emerge no longer reflects the pass/fail status of the last ebuild. As a result the message sent reports PASS even if an ebuild failed. The log excerpt will still clue you in if there was a failure (e.g., "(5 of 17) app-who/badwolf").

I'll look into it soon and either find a more reliable test or file a bug on Portage (if warranted).
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
zeroplus
n00b
n00b


Joined: 19 Mar 2012
Posts: 1

PostPosted: Mon Mar 19, 2012 4:18 am    Post subject: Reply with quote

bobspencer123 wrote:
Hey this looks like a great little script. Thanks so much for your effort.

I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.


I realize this is a bit late but in my frustration with waiting for undefined amounts of time led me to make a similar script :) "run_with_email_confirm" (a bit of a mouthful i know heh).

Code:
#!/bin/bash

if [ $# -lt 1 ]; then
   echo "Usage: $0 <COMMANDS>"
   exit 1;
fi

OUTPUT_TEMP_FILE=/etc/output.log

echo RUNNING:
count=1
for arg in "$@"; do
   echo "#${count}: ${arg}"
done

for arg in "$@"; do

   $arg | tee ${OUTPUT_TEMP_FILE}
   EXIT_STATUS=${PIPESTATUS[0]}
   if [ $EXIT_STATUS -ne 0 ] ; then
      COMMAND="${arg}"
      RESULT=$EXIT_STATUS
      OUTPUT=$(tail -n 30 "${OUTPUT_TEMP_FILE}")
      echo "EXIT STATUS: ${EXIT_STATUS}"
      email_results "${COMMAND}" "${RESULT}" "${OUTPUT}"
      rm "${OUTPUT_TEMP_FILE}"
      exit ${EXIT_STATUS}
   else
      echo "EXIT STATUS: ${EXIT_STATUS}"
   fi   

done

email_results "${FULL_COMMAND}" 0

rm "${OUTPUT_TEMP_FILE}"

exit 0


If you read through the script closely you'll notice that I'm both printing to the console and saving STDOUT and STDERR to a log file.

The script basically does this:

  • take commands ie:
    Code:
    run_with_email_confirm "emerge --sync" "emerge -u world"

  • separate the commands (each of which is just an argument)
  • run them one at a time
  • if it passes then run the next command (if it passes all command arguments, then send an email with a PASS and indicate the commands run)
  • if it fails then take the command that failed ${arg}, exit code ${PIPESTATUS[0]}, last 30 lines of console output ${OUTPUT_TEMP_FILE} and pass it to the email script "email_results" which basically makes it pretty.

The script "email results"[/list] is as follows:

Code:

#!/bin/bash

if [ $# -lt 2  ] || [ $# -gt 3 ]; then
   echo "Usage: $0 <COMMAND> <RESULT> <OUTPUT: optional>"
   exit 1;
fi

EMAIL="<your email goes here>"

COMMAND=$1
EXIT_STATUS=$2

if [ $2 -eq 0 ]; then
   RESULT="PASS"
else
   RESULT="FAIL"
fi

OUTPUT=$3


{
echo "<table>"
echo "<tr>"
echo "<td width=\"100\"><b>DEVICE</td><td>: </td></b> <td>$HOSTNAME</td>"
echo "</tr>"
echo "<tr>"
echo "<td width=\"100\"><b>COMMAND</td><td>: </td></b> <td>$COMMAND<br></td>"
echo "</tr>"
if [ $EXIT_STATUS == 0 ]; then
   echo "<tr>"
   echo "<td width=\"100\"><b>RESULT</td><td>: </td></b> <td>PASS</td>"
   echo "</tr>"
   echo "</table>"
else
   echo "<tr>"
   echo "<td width=\"100\"><b>RESULT</td><td>: </td></b> <td>FAIL</td>"
   echo "</tr>"
   echo "<tr>"
   echo "<td width=\"100\"><b>CODE</td><td>: </td></b> <td>$EXIT_STATUS</td>"
   echo "</tr>"
   echo "</table><br>"
   echo -e "<td><b>OUTPUT:</b><br><br>"
   echo "$OUTPUT<br>"
fi
} | mutt -e "set content_type=text/html" -s "$HOSTNAME: $RESULT notification" $EMAIL


I apologize for all the hack html tags in there...there's probably a better way to handle it. The email wouldn't take terminal color codes etc since it's not a terminal interpretting them so I had to go with html.

You can follow this link to set up gentoo to send from your own gmail address:

http://en.gentoo-wiki.com/wiki/Gmail_and_sSMTP

The result of all this would look something like this:

DEVICE : <HOSTNAME>
COMMAND : <COMMANDS_RUN> (or the command that it failed at)
RESULT : PASS or FAIL
CODE : 141

OUTPUT:

<the console output is shown here only if the thing failed>

Maybe this'll be useful to someone else in the future. The code could be cleaned up indeed etc but hey, it works. I'm working on an embedded device (Beagleboard-XM) so I have to leave it plugged in on top of the router and I can't monitor it 24/7 over serial console right.

UPDATE: I was stupid and forgot that my $RESULT variable is actually the same as $EXIT_STATUS after I changed the script up at one point; meaning...$RESULT is pretty useless..meh
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3182

PostPosted: Mon Mar 19, 2012 7:22 am    Post subject: Reply with quote

timeBandit wrote:
BonezTheGoon wrote:
Awesome work! Thanks so much for sharing!!
:D Thanks! Sorry for not responding sooner, this thread fell off my egosearch due to age.

bobspencer123 wrote:
I was wondering if this could be edited to include the ability to send an email...
Er...that's what it does. :wink: Conversion to SMS happens at the carrier's gateway address, not in the script. Just change SMS_ADDR to the email address where you want to send the note.

Note: The script is partially broken as of Portage 2.1.6.13 and later (at least in the 2.1.x series). The exit status of emerge no longer reflects the pass/fail status of the last ebuild. As a result the message sent reports PASS even if an ebuild failed. The log excerpt will still clue you in if there was a failure (e.g., "(5 of 17) app-who/badwolf").

I'll look into it soon and either find a more reliable test or file a bug on Portage (if warranted).
It might be overkill for something so simple, but my script (see my signature below) does keep track of the number of failed packages and the number of skipped packages. You could hook up your script to be executed at the end of the emerge process (via a config variable POST_CMD). Since your script will be run from inside my script, it will actually be able to access the internal variables of my script, in particular the array exit_status which contains the number of failed and skipped packages.
_________________
emerge --quiet redefined | E17 vids: I, II
Back to top
View user's profile Send private message
midnightnerd
n00b
n00b


Joined: 28 Dec 2012
Posts: 24
Location: UT

PostPosted: Mon Mar 25, 2013 3:24 pm    Post subject: Portage feature request... Reply with quote

Hey, I get that this is an old topic but has it been submitted as a feature request to Portege?
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Mar 25, 2013 3:42 pm    Post subject: Reply with quote

No real need, in my opinion. A trivial wrapper script or bash alias will integrate this little script into your normal Portage workflow. ;)

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks 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