SwarmApps Code Blog

December 20, 2009

Sending short alert emails from C++

Filed under: c++, General Code — Tags: , , , , , — swarmapps @ 4:34 am

I am a coding-army of one. It’s just me trying to realize the visions of cool apps that dance in my head throughout the day.

This has it’s advantages (awesome flexibility) and disadvantages (slooowwww progress..). One problem I have is monitoring my servers throughout the day and night. I obviously don’t have a team of sys admins watching my server performance all day long and monitoring my network topology. But, since I am building commercial apps, I do need to get alerts when something terrible has gone wrong. Hopefully, by planning ahead I’ll then be able to fix the problem as quickly as possible (like having ssh available on my iPhone!)

There are two ways that are a sure-fire way to alert me quickly, email or a text message. It turns out, one simple command in a C++ program can send both to me using standard libraries:

system(“echo ‘Help I have crashed and I cannot get back up!’ | sendmail myemail@mailserver.com”);

Now this requires your server to both allow “system()” calls and to have the standard command line tool “sendmail” set up properly.  I HIGHLY recommend you test both things before relying on this for an emergency call (see code snippet below)!

This can be even more useful if your mobile service provider has set up email-to-text services.  Luckily, most of the major providers have this up and running.  You simply replace the email address with the address you need to send to your phone:

system(“echo ‘Help I have crashed and I cannot get back up!’ | sendmail 2125551212@txt.myprovider.com”);

Like magic, you’ll receive an instant alert that will send you scrambling away from almost any social interaction to try and solve!

I place these alerts in my SIGTERM and SIGINT signal catchers.

Be warned, however, if your program gets sent a SIGKILL, hangs somewhere unusual, or your server hardware simply crumps somehow, you WILL NOT GET AN ALERT!  This is where you need a redundant, external, monitoring program running on separate hardware, power, and network connection as a belt-and-suspenders check.

You did build an interface in your mission-critical server for an external monitoring program to connect to didn’t you?

A little snippet of standalone test code you should be able to compile is shown below.  Remember to change the email address to match your own!

#include <stdlib.h>

int main(int argc, char * argv[]) {
        system("echo 'c++ test' | sendmail myemail@myserver.com");
        return 0;
}

Happy coding!

Mike

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.