View previous topic :: View next topic |
Author |
Message |
timeBandit Bodhisattva
Joined: 31 Dec 2004 Posts: 2719 Location: here, there or in transit
|
Posted: Fri Feb 27, 2009 12:27 am Post subject: TIP: Send SMS alerts when emerge completes |
|
|
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 |
|
|
Bones McCracker Veteran
Joined: 14 Mar 2006 Posts: 1611 Location: U.S.A.
|
Posted: Fri Feb 27, 2009 8:58 am Post subject: |
|
|
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 |
|
|
YourNameInHere n00b
Joined: 28 Mar 2007 Posts: 8
|
Posted: Fri Feb 27, 2009 10:19 am Post subject: |
|
|
what sms gateway do you use? Is there something for free to use?
thx
YourNameInHere |
|
Back to top |
|
|
Bones McCracker Veteran
Joined: 14 Mar 2006 Posts: 1611 Location: U.S.A.
|
Posted: Fri Feb 27, 2009 10:47 am Post subject: |
|
|
my cell phone provider has a gateway |
|
Back to top |
|
|
timeBandit Bodhisattva
Joined: 31 Dec 2004 Posts: 2719 Location: here, there or in transit
|
Posted: Fri Feb 27, 2009 5:16 pm Post subject: |
|
|
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 |
|
|
comprookie2000 Retired Dev
Joined: 25 Jul 2004 Posts: 925 Location: Sun City Center, Florida
|
|
Back to top |
|
|
Bones McCracker Veteran
Joined: 14 Mar 2006 Posts: 1611 Location: U.S.A.
|
Posted: Fri Mar 13, 2009 11:55 pm Post subject: |
|
|
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 |
|
|
milomak Apprentice
Joined: 10 Apr 2008 Posts: 287
|
Posted: Mon Mar 16, 2009 5:34 pm Post subject: |
|
|
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 |
|
|
.yankee Apprentice
Joined: 24 Feb 2008 Posts: 194 Location: Polska
|
Posted: Mon Mar 23, 2009 12:18 am Post subject: |
|
|
Nice trick... But isn't it easier to use --keep-going emerge parameter? |
|
Back to top |
|
|
Bones McCracker Veteran
Joined: 14 Mar 2006 Posts: 1611 Location: U.S.A.
|
Posted: Mon Mar 23, 2009 12:24 am Post subject: |
|
|
.yankee wrote: | Nice trick... But isn't it easier to use --keep-going emerge parameter? |
Yeah. Or you could just use Ubuntu. |
|
Back to top |
|
|
coolsnowmen Veteran
Joined: 30 Jun 2004 Posts: 1479 Location: No.VA
|
Posted: Mon Mar 23, 2009 5:41 pm Post subject: |
|
|
BoneKracker wrote: | .yankee wrote: | Nice trick... But isn't it easier to use --keep-going emerge parameter? |
Yeah. Or you could just use Ubuntu. |
You do know you are on forums.gentoo.org right? _________________ emerge: there are no ebuilds to satisfy "moo" |
|
Back to top |
|
|
timeBandit Bodhisattva
Joined: 31 Dec 2004 Posts: 2719 Location: here, there or in transit
|
Posted: Mon Mar 23, 2009 5:45 pm Post subject: |
|
|
.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 |
|
|
Bones McCracker Veteran
Joined: 14 Mar 2006 Posts: 1611 Location: U.S.A.
|
Posted: Mon Mar 23, 2009 6:59 pm Post subject: |
|
|
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. |
You do know you are on forums.gentoo.org right? |
You do know that was joke, right? |
|
Back to top |
|
|
polovinamozga n00b
Joined: 11 Sep 2008 Posts: 13
|
Posted: Wed Mar 25, 2009 4:48 am Post subject: |
|
|
great script! |
|
Back to top |
|
|
.yankee Apprentice
Joined: 24 Feb 2008 Posts: 194 Location: Polska
|
Posted: Sun Mar 29, 2009 1:59 pm Post subject: |
|
|
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 |
|
|
timeBandit Bodhisattva
Joined: 31 Dec 2004 Posts: 2719 Location: here, there or in transit
|
Posted: Wed Apr 08, 2009 2:21 pm Post subject: |
|
|
.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 ). 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 |
|
|
BonezTheGoon Bodhisattva
Joined: 14 Jun 2002 Posts: 1408 Location: Albuquerque, NM -- birthplace of Microsoft and Gentoo
|
Posted: Tue Feb 16, 2010 4:46 pm Post subject: |
|
|
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!! _________________
pjp wrote: | The greater evil is voting for the "lesser evil." |
|
|
Back to top |
|
|
bobspencer123 Guru
Joined: 19 Dec 2005 Posts: 544
|
Posted: Tue Feb 16, 2010 4:55 pm Post subject: |
|
|
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 |
|
|
lazy_bum l33t
Joined: 16 Feb 2005 Posts: 691
|
Posted: Wed Feb 17, 2010 10:04 pm Post subject: |
|
|
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 |
|
|
timeBandit Bodhisattva
Joined: 31 Dec 2004 Posts: 2719 Location: here, there or in transit
|
Posted: Thu Feb 25, 2010 2:19 pm Post subject: |
|
|
BonezTheGoon wrote: | Awesome work! Thanks so much for sharing!! | 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. 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 |
|
|
zeroplus n00b
Joined: 19 Mar 2012 Posts: 1
|
Posted: Mon Mar 19, 2012 4:18 am Post subject: |
|
|
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:
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 |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
Posted: Mon Mar 19, 2012 7:22 am Post subject: |
|
|
timeBandit wrote: | BonezTheGoon wrote: | Awesome work! Thanks so much for sharing!! | 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. 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 | Now using kde5 | e is unstable :-/ |
|
Back to top |
|
|
midnightnerd n00b
Joined: 28 Dec 2012 Posts: 26 Location: UT
|
Posted: Mon Mar 25, 2013 3:24 pm Post subject: Portage feature request... |
|
|
Hey, I get that this is an old topic but has it been submitted as a feature request to Portege? |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10702 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Mar 25, 2013 3:42 pm Post subject: |
|
|
No real need, in my opinion. A trivial wrapper script or bash alias will integrate this little script into your normal Portage workflow.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
|