19.06.2015 Views

ADMIN

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

NUTS AND BOLTS<br />

Daemon Monitoring<br />

© Shariff Che'Lah, 123RF.com<br />

Monitoring daemons with shell tools<br />

Watching the<br />

Daemons<br />

Administrators often write custom monitoring programs to make sure<br />

their daemons are providing the intended functionality. But simple shell<br />

tools are just as well suited to this task, and not just for systems that are<br />

low on resources. By Harald Zisler<br />

Unix daemons typically go about<br />

their work discreetly in the background.<br />

The process table, which is<br />

output with the ps command, only<br />

shows you that these willing helpers<br />

have been launched, although in the<br />

worst case they could just be hanging<br />

around as zombies. Whether or<br />

not a daemon is actually working is<br />

not something that the process table<br />

will tell you. In other words, you<br />

need more granular diagnostics. The<br />

underlying idea is to write a “sensor”<br />

script for each service that performs a<br />

tangible check of its individual functionality.<br />

Because almost every program outputs<br />

standardized exit codes when it<br />

terminates, you can use Unix conventions.<br />

0 stands for error-free processing,<br />

whereas 1 indicates some problems<br />

were encountered. This value is<br />

stored in the $? shell variable, which<br />

a shell script evaluates immediately<br />

after launching the sensor.<br />

Various programs are suitable for<br />

automated, “unmanned” access to<br />

the service provided by a given daemon;<br />

all of them will run in the shell<br />

without a GUI. These programs often<br />

provide an option (typically -q) that<br />

suppresses output, and this is fine<br />

for accessing the exit code. Error logs<br />

are obtainable by redirecting the error<br />

output to a file or, if available, by<br />

setting the corresponding program<br />

option.<br />

The only thing left to do is to find<br />

the matching client program test the<br />

functionality of each service.<br />

Web Servers<br />

To check a web server, you could use<br />

wget. The shell script command line<br />

for this would be:<br />

wget --spider -q ip-address<br />

The --spider option tells wget to<br />

check that the page exists but not to<br />

load it. Defining the IP address instead<br />

of the hostname avoids a false<br />

positive if DNS-based name resolution<br />

fails for some reason.<br />

Listing 1: Database Monitoring<br />

01 #! /bin/sh<br />

02<br />

03 while true<br />

04 do<br />

05<br />

06 zeit=$(date +%d.%m.%y\ %H:%M\ )<br />

07<br />

08 psql -U monitor -d monitor<br />

-c "select * from watch;"<br />

09<br />

10 if [ $? -eq 2 ];<br />

11<br />

12 then<br />

13<br />

14 echo " $time: Database is not accessible!<br />

****************" >> dba.log<br />

15 /usr/local/etc/rc.d/002pgsql.sh start<br />

16 sleep 15<br />

17 psql -U monitor -d monitor<br />

-c "select * from watch;"<br />

18<br />

19 if [ $? -eq 0 ];<br />

20<br />

21 then<br />

22<br />

23 echo " $time: Database online!<br />

+++++++++" >> dba.log<br />

24<br />

25 else<br />

26<br />

27 echo " $time: Database: serious error!<br />

***************" >> dba.log<br />

28 echo " $time: Unable to restart!<br />

****************" >> dba.log<br />

29 while true<br />

30 do<br />

31 psql -U monitor -d monitor<br />

-c "select * from watch;"<br />

32<br />

33 if [ $? -eq 0 ];<br />

34<br />

35 then<br />

36<br />

37 time=$(date +%d.%m.%y\ %H:%M\ )<br />

38 echo " $time: Database online!<br />

+++++++++" >> dba.log<br />

39 break<br />

40<br />

41 fi<br />

42 sleep 15<br />

43 done<br />

44<br />

45 fi<br />

46<br />

47 fi<br />

48 sleep 15<br />

49<br />

50 done<br />

92 <strong>ADMIN</strong> 01 WWW.<strong>ADMIN</strong>-MAGAZINE.COM

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!