Solved

Monitoring system health on linux

Posted on 2006-06-19
15
322 Views
Last Modified: 2013-12-06
I am running Red Hat EL 4, and I need to check the CPU useage, memory useage, disk useage, and network useage and write those values to an XML file as a percentage of total capacity.

The format needs to be something like this:
<cpu>48.0</cpu>
<ram>32.5</ram>

The file needs to be updated with new values every 5 seconds or so. Any ideas?
0
Comment
Question by:Thanatos2k
15 Comments
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
Yes....lots of ideas;)
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 125 total points
Comment Utility
Disc bit is easy:

df | grep /dev/hda2 | awk {'print "<disc>" $5 "</disc>"'}
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
RAM not too difficult:

cat /proc/meminfo | grep "Mem:" | awk '{t=int($2);u=($3); printf "<ram>%02.0f\%</ram>\n", (100*u/t)}'
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
cpu idle is reasonably easy:

top -b -n1 | sed -n '6p' | awk {'print "cpu>" $8+0 "</cpu>"'}

(The +0 forces it to get rid of the %. Note that top -b, seems to add 2 extra lines on my test system, you may need to correct this for your system by changing the 6p to 4p, or whatever by trial and error.

Total cpu is also not to bad:

top -b -n1 | sed -n '6p' | awk {'print "cpu>" $2+$3+$4+$5+$6+$7 "</cpu>"'}

Now to put into a suitable script:)



0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
report.sh:

-------8X---------
#!/bin/bash

while true; do
        df | grep /dev/hda2 | awk {'print "<disc>" $5 "</disc>"'}
        cat /proc/meminfo | grep "Mem:" | awk '{t=int($2);u=($3); printf "<ram>%02.0f\%</ram>\n", (100*u/t)}'
        top -b -n1 | sed -n '6p' | awk {'print "<cpu>" $2+$3+$4+$5+$6+$7 "%</cpu>"'}
sleep 300
done
-------8X---------

Obviously it takes time to run the commands, so it isn't exactly synchronised......

./report.sh > logfile.txt

Obviously, you might decide to add so date stamping in some form with the date command?

However...you may wish to look at nagios:

www.nagios.org

or mon:

www.kernel.org/software/mon

before trying to re-invent the wheel? Alternatively, as they are open source, you may pick up a few nice ideas from them:)

With respect to network monitoring, there is no % useage that I am aware of. What you could do is over a period of time record the number of packets sent, (cat /proc/net/dev), and then look at the number of packets sent in a 5 minute period as a percentage of a theorectical maximum, but this would not be realistic, so you'll have to have a bit of a re-think as to exactly what you'd like to record there.

HTH:)

0
 
LVL 16

Assisted Solution

by:xDamox
xDamox earned 125 total points
Comment Utility
Hi,

CPU usage can be captured by doing:

top | awk '/Cpu\(s\)/ { print "<cpu>", $2, "</cpu>" }'

The Memory usage can be captured by doing:

top | awk '/Mem:/ { print "<memory>",$5,"</memory>" }'

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Assisted Solution

by:owensleftfoot
owensleftfoot earned 125 total points
Comment Utility
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
Worth noting that xDamox's solutions don't work for me as my top output is:
-------------------8X---------------------------------
103 processes: 100 sleeping, 3 running, 0 zombie, 0 stopped
CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
           total    0.3%    0.0%    0.0%   0.0%     0.0%    0.0%   99.6%
Mem:   513504k av,  497544k used,   15960k free,       0k shrd,  224284k buff
                    377012k actv,   71784k in_d,    6988k in_c
Swap: 1044216k av,  122120k used,  922096k free                   98408k cached
-------------------8X---------------------------------

My tests were carried out on a RHEL3 server, so it is possible that RHEL4 may be slightly different? The same applies to the /proc/

paths, as the names may have changed slightly in RHEL4?
0
 
LVL 16

Expert Comment

by:xDamox
Comment Utility
For your information pjedmond mine was tested on: Fedora

top - 23:54:39 up 2 days, 10:44,  2 users,  load average: 0.35, 0.25, 0.09
Tasks: 105 total,   1 running, 104 sleeping,   0 stopped,   0 zombie
Cpu(s):  7.8% us,  1.6% sy,  0.3% ni, 89.9% id,  0.4% wa,  0.0% hi,  0.0% si,  0.0% st
Mem:    515764k total,   379384k used,   136380k free,    21396k buffers
Swap:  1052248k total,    75996k used,   976252k free,   228492k cached
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
xDamox - Can I ask which version of FC?
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
Always nice to have a sanity check to look out for mistakes and spurious differences. Morale of the story is that Thanatos2k is going to have to understand the scripts before using them;)
0
 
LVL 16

Expert Comment

by:xDamox
Comment Utility
pjedmond Fedora Core 5
0
 
LVL 9

Assisted Solution

by:gtkfreak
gtkfreak earned 125 total points
Comment Utility
Try out ksysguard in case you are using KDE Desktop or gnome-system-monitor in case you are using the Gnome Desktop
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now