Load Average and CPU usage in Linux!

Load Average and CPU usage in Linux
====================================

The three common commands that give the load average details are:

* uptime
* w
* top

* tload graphical but in terminal
* xload graphical in the X display

It is the average sum of the number of processes waiting in the run-queue plus the processes currently executing over 1, 5, and 15 minute time periods.

It represents the average number of processes that are in the running (using the CPU) or runnable (waiting for the CPU) states. The notable exception in Linux is that it includes processes in uninterruptible sleep states like waiting for some I/O activity to complete. This can markedly increase the load average on Linux systems.

For a single processor machine a load average of 1 means that, on average, there is always a process in the running or runnable state. Thus, the CPU is being utilized 100% of the time and is at capacity. If you tried to run another process, it would have to wait in the run queue before being executed. For multiprocessor systems, however, the system isn’t CPU bound until the load average equals the number of processors (or cores, for multi-core processors) in the machine. If a database server, for example, has two dual core processors(2×2 processors), the system isn’t fully utilized until the load average reaches 4.

As a general rule of thumb we dont have to panic untill the load average is 3 (3 for single core 6 for dual core and so on ). This is strictly a general concept.

CPU persentage or CPU usage
===========================

CPU percentage is the amount of a time interval that the system’s processes were found to be active on the CPU. If the system CPU usage (by using top command) is 45%, 45% of the samples taken by top found your process active on the CPU. The rest of the time the application was in a wait. (It is important to remember that a CPU is a discrete state machine. It really can be at only 100%, executing an instruction, or at 0%, waiting for something to do. There is no such thing as using 45% of a CPU. The CPU percentage is a function of time.)

To summarize CPU load is no. of processes waiting or running in the processor. CPU usages is time taken by any process in the CPU before complete execution.

Continue Reading

Chapter 5 Logging to a Linux system!

Linux is a Multi-user and Multi-tasking operating system. The system can be used by more than one user simultaneously, and the computer can also run multiple programsat a time.

Types of Multi-tasking :-

1. Cooperative multitasking – Decision is taken by application/programs cooperatively. A poorly designed program or a crashed process can potentially crash the entire operating system since it could prevent a process from relinquishing the CPU to other processes.

2. Preemptive multitasking – Decision is taken by OS. Permits the system to respond immediately to important external events, such as incoming data from a keyboard or network.

Types of Logins :-

  • Command Line login
  • GUI based login

Case-Sensitivity in login : fossil and FOSSIL are two different user names! The same applies to the passwords!

Virtual Consoles -

The linux based system provides different virual consoles for login.
Alt+F1 ,Alt+F2 till Alt+F6 (by default) sometimes it is Ctrl+Alt+F1 allows you to multi-task in console mode also. Alt + F7 is the GUI

How to login:-

  • Console – ttys and pts    (ttys is Terminal Type and  pts is  pseudo terminal).
  • Serial Terminal – Hyperterminal
  • Network Connection – SSH / telnet etc( connects between the systems remotely )

CUI  :- CUI (Composite User Interface) is a compound type of user interface which incorporates the features of the GUI (Graphical User Interface) and the CLI (Command Line Interface) by realizing both interface in one area at the same time

GUIs In Linux :-

Unlike Windows, GUI is an application in itself.

XFree86, Xorg  are two free X server implementations and VNC – client/server architecture The Client and Server communicates using TCP/IP protocol even in local system and not the usual way of local communication, Unix sockets.

In Linux we have a  dozens of choices for  GUI:-

Desktops:-

  • GNOME
  • KDE
  • XFCE
  • CDE

Window Managers – Clients application of X :-

  • FVWM
  • IceWM
  • FluxBOX
  • Enlightenment
  • BlackBox
  • Metacity – GNOME’s WM
Continue Reading

Monitoring with Nagios and NRPE

Nagios

Nagios is a versatile and functional network management tool with a GUI (graphicuser interface) comparable to other commercial tools. It is a system and network monitoring application. It watches hosts and services that you specify, alerting you when things go bad and when they get better. Nagios was originally designed to run under Linux, but now it also runs well on other Unix variants.

Nagios Installation Guides:

This blog is intended to provide you with simple instructions on how to install Nagios from source (code) in Fedora.

Prerequisites:

Before installing Nagios,you need to install the following packages:
Apache
GCC compiler
GD development libraries

You can use yum to install these packages by running the following commands (as root):

yum install  httpd
yum install  gcc
yum install  glibc glibc-common
yum install  gd gd-devel

1) Create Account Information

Create a new nagios user account.

/usr/sbin/useradd -m nagios

Create a new nagcmd group for allowing external commands to be submitted through the web interface. Add both the nagios user and the apache user to the group.

/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache

2) Download Nagios:

Create a directory for storing the downloads.

mkdir ~/ssages
cd ~/ssages
wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gz

3) Compile and Install Nagios

Extract the Nagios source code tarball.

tar xzf nagios-3.0.6.tar.gz
cd nagios-3.0.6
./configure –with-command-group=nagcmd
make install
make install-init
make install-config
make install-commandmode
nagios-3.0.6

4) Configure the Web Interface

Install the Nagios web config file in the Apache conf.d directory.

make install-webconf

Create a nagiosadmin account for logging into the Nagios web interface.

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Edit the file /usr/local/apache/conf/includes/pre_virtualhost_global.conf and append the following lines to the virtual host directories.

AuthName “Nagios”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user

Restart Apache to make the new settings take effect.

service httpd restart

5) Compile and Install the Nagios Plugins

wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz

cd ~/ssages

tar xzf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11

Compile and install the plugins.

./configure –with-nagios-user=nagios –with-nagios-group=nagios
make
make install

6) Start Nagios

Add Nagios to the list of system services and have it automatically start when the system boots.

chkconfig –add nagios
chkconfig nagios on

Verify the sample Nagios configuration files.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, start Nagios.

service nagios start

you can check the Nagios in your browser typing the following url:

http://localhost/nagios

You should now be able to access the Nagios web interface at the URL below. You’ll be prompted for the username (nagiosadmin) and password you specified earlier.

NRPE Installation and Configuration

The NRPE addon is designed to allow you to execute Nagios plugins on remote Linux/Unix machines. The main reason for doing this is to allow Nagios to monitor “local” resources (like CPU load, memory usage, etc.) on remote machines. Since these public resources are not usually exposed to external machines, an agent like NRPE must be installed on the remote Linux/Unix machines.

The NRPE addon consists of two pieces:

The check_nrpe plugin, which resides on the local monitoring machine.
The NRPE daemon, which runs on the remote Linux/Unix machine.

When Nagios needs to monitor a resource of service from a remote Linux/Unix machine:
Nagios will execute the check_nrpe plugin and tell it what service needs to bechecked.
The check_nrpe plugin contacts the NRPE daemon on the remote host over an (optionally) SSL-protected connection.
The NRPE daemon runs the appropriate Nagios plugin to check the service or resource.
The results from the service check are passed from the NRPE daemon back to thecheck_nrpe plugin, which then returns the check results to the Nagios process.

INSTALLATION

Remote Host Setup:

Create a new nagios user account.

/usr/sbin/useradd nagios

Create a directory for storing the downloads.

mkdir ~/ssages
cd ~/ssages

wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz

Extract the Nagios plugins source code tarball.

tar xzf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11

Compile and install the plugins.

./configure
make
make install

The permissions on the plugin directory and the plugins will need to be fixed at this point, for this run the following commands.

chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec

Install the NRPE daemon:

cd ~/ssages
wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nrpe-2.8.tar.gz

tar xzf nrpe-2.8.tar.gz
cd nrpe-2.8
Compile the NRPE addon.
./configure
make all

Install the NRPE plugin (for testing), daemon, and sample daemon config file.

make install-plugin

make install-daemon

make install-daemon-config

Add the following entry for the NRPE daemon to the /etc/services file.

nrpe            5666/tcp# NRPE

Next we add an init script for nrpe. For this,

cd nrp-2.12/
cp ./src/nrpe /usr/sbin/
cp ./sample-config/nrpe.cfg /etc /
cat init-script.in > /etc/init.d/nrpe
mod a+x /etc/init.d/nrpe
open  the /etc/init.d/nrpe and edit
NrpeBin=/usr/sbin/nrpe
NrpeCfg=/usr/local/nagios/etc/nrpe.conf

Then restart nrpe services

/etc/init.d/nrpe restart

Then edit the nrpe configuration file and allow the monitoring hoast ipaddress.

allowed_hosts=ipaddress of monitoring host

Next to open nrpe port in the firewall.

vi  /etc/apf/conf.apf

edit the follwing section and add the port number 5666

IG_tcp_cports=”5666,20………..etc”

Restart the services

/etc/init.d/apf restart

Make sure the nrpe daemon is running under xinetd.

netstat -at | grep nrpe

The output out this command should show something like this:

tcp         0           0 *:nrpe    *:*          LISTEN

Monitoring Host Setup

For monitoring remote host, you need to install nrpe plugin to the monitoring host. For this,

cd ~/ssages
wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nrpe-2.8.tar.gz

tar xzf nrpe-2.8.tar.gz
cd nrpe-2.8/

Compile the NRPE addon.

./configure
make all

Install the NRPE plugin.

make install-plugin

Create a command definition

vi /usr/local/nagios/etc/commands.cfg

and add the following definition to the file:

define command{
command_name         check_nrpe
command_line         $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

You can insert each remote system services in one file. For that you have to edit the configuration file

vi /usr/local/nagios/etc/nagios.cfg

and append the following line.

cfg_file=/usr/local/nagios/etc/objects/remotehost.cfg

Create host and service definitions

First create a new template for each different type of host you’ll be monitoring. Let’s create a new template for linux boxes.
edit /usr/local/nagios/etc/objects/templates.cfg and add the following lines,

define host{
name                  linux-box             ; Name of this template
use                   generic-host          ; Inherit default values
check_period          24×7
check_interval        5
retry_interval        1
max_check_attempts    10
check_command         check-host-alive
notification_period   24×7
notification_interval 30
notification_options  d,r
contact_groups        admins
register              0 ; DONT REGISTER THIS – ITS A TEMPLATE
}

Notice that the linux-box template definition is inheriting default values from the generic-host template, which is defined in the localhost.cfg file.

Next, define a new host for the remote Linux/Unix box that references the newly created linux-box host template.
For this edit /usr/local/nagios/etc/objects/remotehost.cfg

define host{
use       linux-box ; Inherit default values from a template
host_name remotehost ; The name we’re giving to this server
address   192.168.0.1 ; IP address of the server
check_command check-host-alive
}

Then define contact name and contact goup name in same file

define contact{
contact_name clientcontact
host_name remote_host
use generic-contact
alias Nagios client
email remotehost@gmail.com
}
define contactgroup{
contactgroup_name groupname
host_name remote_host
alias Nagios group
members clientcontact,membersof remote_host
}

Now define some services to monitor the remote Linux/Unix box. These service definitions will use the commands that have been defined in the nrpe.cfg file in the remote host.

The following service will monitor the CPU load in the remote host. The “check_load” argument that is passed to the check_nrpe command definition tells the NRPE daemon to run the “check_load” command as defined in the nrpe.cfg file.

define service{
use                 generic-service
host_name           remotehost
service_description CPU Load
check_command       check_nrpe!check_load
}

The following service will monitor the the number of currently logged-in users in the remote host.

define service{
use                         generic-service
host_name                   remotehost
service_description         Current Users
check_command               check_nrpe!check_users
}

The following service will monitor the free drive space on /dev/hda1 in the remote host.

define service{
use                         generic-service
host_name                   remotehost
service_description         /dev/hda1 Free Space
check_command               check_nrpe!check_hda1
}

The following service will monitor the total number of processes in the remote host.

define service{
use                        generic-service
host_name                  remotehost
service_description        Total Processes
check_command              check_nrpe!check_total_procs
}

The following service will monitor the number of zombie processes in the remote host.

define service{
use                        generic-service
host_name                  remotehost
service_description        Zombie Processes
check_command              check_nrpe!check_zombie_procs
}

The following service will monitor http status in the remote host.

define service{
use generic-service
host_name remote_host
service_description HyperVM
check_command check_nrpe!check_http
}

Restarting Nagios:

Verify your Nagios configuration files.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are errors, fix them. If everything is fine, restart Nagios.

service nagios restart

Remote Host Configuration:

Edit the nrpe configuration file and add the folowing lines:

vi /usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_http]=/usr/local/nagios/libexec/check_http -H 127.0.0.1 -w 5 -c 10

Restart the nrpe:

/etc/init.d/nrpe restart

Continue Reading

Some of my favorite quotations on Linux,Unix and Open Source philosophy

Unix is extremely user friendly. It also happens to be extremely selective when picking its friends

Isn’t it ? That could be the reason why many don’t like it. Linux don’t want to be friends with them.

Knowledge isn’t anybody’s private property

and we value that. We share whatever we know and also encourage all our staff to do so.

If I am not for myself, who will be for me?
If I am only for myself, what am I?
If not now, when?

Closed source should die!

Avoid the Gates of Hell. Use Linux

You don’t have to tell us that!!!

No fences, No Gates!

World is free!!

For the brave, there is a door named Linux to success. For the rest there are windows.

Really, I’m not out to destroy Microsoft. That will just be a completely unintentional side effect.

Intelligence is the ability to avoid doing work, yet getting the work done.

Software is like Sex, It’s better when it is FREE

Make using your computer a joy and not a hell loaded with tons of viruses and spyware.

People aren’t as dumb as Microsoft needs them to be and that’s our success.

To mess up a Linux box, you need to work at it; to mess up your Windows box, you just have to work on it.

Windows is a bonfire, Linux is the sun. Linux only looks smaller if you lack perspective

Teamwork is essential — it allows you to blame someone else.

If Linux doesn’t have the solution, you have the wrong problem

Or you don’t know Linux.

Programming is like sex… make one mistake, and support it the rest of your life

Microsoft and Apple make the easy things very easy and the hard things impossible. BSDs and Linux make the easy things challenging and the hard things difficult but possible

Linux is for people who want to know why it works.
Mac is for people who don’t want to know why it works.
DOS is for people who want to know why it does not work.
Windows is for people who don’t want to know why it does not work.

A Windows user spends 1/3 of his life sleeping, 1/3 working, 1/3 waiting

Linux means productivity and fun. NT means ‘Not Today’.

Love is Hate. War is Peace. Windows is stable.

“Unix is simple, but it takes a genius to understand the simplicity.” – Dennis Ritchie

Unix is the answer, but only if you phrase the question very carefully.

“…Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and the Ugly).”

When you open Windows…BUGS GET IN!!!

Windows isn’t a virus, viruses do something.

Use Linux: because a PC is a terrible thing to waste

“The box said that I needed to have Windows 98 or better… so I installed Linux”

Operating systems are like underwear. nobody really wants to look at them.

I’m not one of those who think Bill Gates is the devil. I simply suspect that if Microsoft ever met up with the devil, it wouldn’t need an interpreter.

No, I will not fix your computer again. USE LINUX

- I used to say it in my college :-P

LINUX – The Best Things In Life Are Free

REAL MEN USE LINUX

Geek by birth, Linux by choice

Linux – Because I am better than you

Open Windows and see the world!! Open Linux and create your own world!!

Continue Reading

Why are you getting 500 internal server error on your php pages

Usually this happens in a suPHP environment. There could be few reasons why this could happen. We list here a few. The solution is intended for those with shell access.

  • Ownership issues
    In a suPHP environment, depending on the mode of suPHP enabled on the VPS or server, there is a min_uid setting, which is configurable in suPHP.conf under /etc or /usr/local/etc or /opt/suphp/etc . By default the setting is 100, which means if the file is owned by a UID less than that value (100), it shouldn’t parse the php code and will result in 500 Internal Server Error. In a cPanel server, the error will be shown @ /usr/local/apache/logs/suphp.log. This restriction would enable better security, since even if a php can do some privilege escalation, it wont affect the server. Solution would be

    chown your_username.your_username path_of_your_php_file

    If that fixes only that file and not your entire php files, use the below command to change all the php files.

    find . -type f -name “*.php” -exec chown username.username {} \;
    find . -type d -exec chown username.username {} \;

    Ignore “*.php” in the above command if it still errors out for a php file. If it still don’t fix, move on to other solutions below.

  • Permission issues
    It could be the world writable directories or files which is causing the issue. In a non-suphp environment, if a php script has to upload a file on a folder, (eg:  php based gallery script where customers are able to upload photos) inorder to get it uploaded to the folder, it needs to have world writable permissions. Or else apache can’t write to the folder. Here in suPHP, since the php is executed as the user itself who already have the permissions you don’t need to change it to 777 permissions and is happy with 755 (rwxr-xr-x) permissions. When a customer is moved from a non suPHP env to suPHP enabled server, we need to adjust the permissions then. Often a command like below should help

    find . -name “*.php” -type f -exec chmod 644 {} \;
    find . -type d -exec chmod 755 {} \;

    Often, you can do the above command without the -name “*.php” in the command.

  • suphp.log exceeds 2GB size.If the entire servers’ php application gives you internal server error, next chance is that the suPHP log has exceeded 2GB space. Just clear the file and you are on track again. To null it, execute the command below. Below command assumes you are having the issue in a cPanel server

    cat /dev/null > /usr/local/apache/logs/error_log

  • suPHP log don’t exist
    In a Plesk or Webmin or other servers, often the suPHP will be installed but /var/log/httpd or such folders where the suPHP.log can be created wont exist. suphp can understand and create a non-existent suphp.log, but you would need to manually create the folder /var/log/httpd. I hope you know how to create a folder though.
  • If you know any other cases which require another solution for this, please comment here or edit the blog.

Continue Reading

About this blog

This blog, acts as a knowledge repository for the world and is unofficial! Anything we find interesting in the cyber world will go here. Most cases, this blog will reflect the happiness of our staff in reaching successful solution to an issue (s)he worked on. A reference for other fellow SAGEs who come across similar issues later