Solved

Monitoring system health on linux

Posted on 2006-06-19
15
330 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
ID: 16936581
Yes....lots of ideas;)
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 125 total points
ID: 16936618
Disc bit is easy:

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

Expert Comment

by:pjedmond
ID: 16936759
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 22

Expert Comment

by:pjedmond
ID: 16936876
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
ID: 16937028
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
ID: 16937050
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
 
LVL 17

Assisted Solution

by:owensleftfoot
owensleftfoot earned 125 total points
ID: 16937095
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16937170
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
ID: 16938609
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
ID: 16938691
xDamox - Can I ask which version of FC?
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16938701
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
ID: 16940104
pjedmond Fedora Core 5
0
 
LVL 9

Assisted Solution

by:gtkfreak
gtkfreak earned 125 total points
ID: 16940775
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to install Ubuntu 16 in DELL venue 8 pro 20 277
Use CRON to save a copy of an open file 1 55
LINUX backups with VEEAM 8 117
How to enable sync between two yum repo? 1 39
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

792 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