Cari di RHE Linux 
    RHE Linux User Manual
Daftar Isi
(Sebelumnya) 13 : Part VI. Monitoring and A ...13 : Chapter 20. Viewing and M ... (Berikutnya)

Deployment Guide

Chapter 19. System Monitoring Tools

In order to configure the system, system administrators often need to determine the amount of free memory, how much free disk space is available, how the hard drive is partitioned, or what processes are running.

19.1. Viewing System Processes

19.1.1. Using the ps Command

The ps command allows you to display information about running processes. It produces a static list, that is, a snapshot of what is running when you execute the command. If you want a constantly updated list of running processes, use the top command or the System Monitor application instead.
To list all processes that are currently running on the system including processes owned by other users, type the following at a shell prompt:
ps ax
For each listed process, the ps ax command displays the process ID (PID), the terminal that is associated with it (TTY), the current status (STAT), the cumulated CPU time (TIME), and the name of the executable file (COMMAND). For example:
~]$ ps ax  PID TTY  STAT   TIME COMMAND 1 ? Ss 0:01 /sbin/init 2 ? S  0:00 [kthreadd] 3 ? S  0:00 [migration/0] 4 ? S  0:00 [ksoftirqd/0] 5 ? S  0:00 [migration/0] 6 ? S  0:00 [watchdog/0][output truncated]
To display the owner alongside each process, use the following command:
ps aux
Apart from the information provided by the ps ax command, ps aux displays the effective username of the process owner (USER), the percentage of the CPU (%CPU) and memory (%MEM) usage, the virtual memory size in kilobytes (VSZ), the non-swapped physical memory size in kilobytes (RSS), and the time or date the process was started. For instance:
~]$ ps auxUSER   PID %CPU %MEM VSZ   RSS TTY  STAT START   TIME COMMANDroot 1  0.0  0.1  19404   832 ? Ss   Mar02   0:01 /sbin/initroot 2  0.0  0.0  0 0 ? S Mar02   0:00 [kthreadd]root 3  0.0  0.0  0 0 ? S Mar02   0:00 [migration/0]root 4  0.0  0.0  0 0 ? S Mar02   0:00 [ksoftirqd/0]root 5  0.0  0.0  0 0 ? S Mar02   0:00 [migration/0]root 6  0.0  0.0  0 0 ? R Mar02   0:00 [watchdog/0][output truncated]
You can also use the ps command in a combination with grep to see if a particular process is running. For example, to determine if Emacs is running, type:
~]$ ps ax | grep emacs12056 pts/3 S+ 0:00 emacs12060 pts/2 S+ 0:00 grep --color=auto emacs
For a complete list of available command line options, refer to the ps(1) manual page.

19.1.2. Using the top Command

The top command displays a real-time list of processes that are running on the system. It also displays additional information about the system uptime, current CPU and memory usage, or total number of running processes, and allows you to perform actions such as sorting the list or killing a process.
To run the top command, type the following at a shell prompt:
top
For each listed process, the top command displays the process ID (PID), the effective username of the process owner (USER), the priority (PR), the nice value (NI), the amount of virtual memory the process uses (VIRT), the amount of non-swapped physical memory the process uses (RES), the amount of shared memory the process uses (SHR), the percentage of the CPU (%CPU) and memory (%MEM) usage, the cumulated CPU time (TIME+), and the name of the executable file (COMMAND). For example:
~]$ toptop - 02:19:11 up 4 days, 10:37,  5 users,  load average: 0.07, 0.13, 0.09Tasks: 160 total,   1 running, 159 sleeping,   0 stopped,   0 zombieCpu(s): 10.7%us,  1.0%sy,  0.0%ni, 88.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%stMem: 760752k total,   644360k used,   116392k free, 3988k buffersSwap:  1540088k total, 76648k used,  1463440k free,   196832k cached  PID USER  PR  NI  VIRT  RES  SHR S %CPU %MEM TIME+  COMMAND14401 jhradile  20   0  313m  10m 5732 S  5.6  1.4   6:27.29 gnome-system-mo 1764 root  20   0  133m  23m 4756 S  5.3  3.2   6:32.66 Xorg13865 jhradile  20   0 1625m 177m 6628 S  0.7 23.8   0:57.26 java   20 root  20   0 0 0 0 S  0.3  0.0   4:44.39 ata/0 2085 root  20   0 40396  348  276 S  0.3  0.0   1:57.13 udisks-daemon 1 root  20   0 19404  832  604 S  0.0  0.1   0:01.21 init 2 root  20   0 0 0 0 S  0.0  0.0   0:00.01 kthreadd 3 root  RT   0 0 0 0 S  0.0  0.0   0:00.00 migration/0 4 root  20   0 0 0 0 S  0.0  0.0   0:00.02 ksoftirqd/0 5 root  RT   0 0 0 0 S  0.0  0.0   0:00.00 migration/0 6 root  RT   0 0 0 0 S  0.0  0.0   0:00.00 watchdog/0 7 root  20   0 0 0 0 S  0.0  0.0   0:01.00 events/0 8 root  20   0 0 0 0 S  0.0  0.0   0:00.00 cpuset 9 root  20   0 0 0 0 S  0.0  0.0   0:00.00 khelper   10 root  20   0 0 0 0 S  0.0  0.0   0:00.00 netns   11 root  20   0 0 0 0 S  0.0  0.0   0:00.00 async/mgr   12 root  20   0 0 0 0 S  0.0  0.0   0:00.00 pm[output truncated]
Table 19.1, "Interactive top commands" contains useful interactive commands that you can use with top. For more information, refer to the top(1) manual page.

Table 19.1. Interactive top commands

CommandDescription
Enter, SpaceImmediately refreshes the display.
h, ?Displays a help screen.
kKills a process. You are prompted for the process ID and the signal to send to it.
nChanges the number of displayed processes. You are prompted to enter the number.
uSorts the list by user.
MSorts the list by memory usage.
PSorts the list by CPU usage.
qTerminates the utility and returns to the shell prompt.

19.1.3. Using the System Monitor Tool

The Processes tab of the System Monitor tool allows you to view, search for, change the priority of, and kill processes from the graphical user interface.
To start the System Monitor tool, either select ApplicationsSystem ToolsSystem Monitor from the panel, or type gnome-system-monitor at a shell prompt. Then click the Processes tab to view the list of running processes.
System Monitor - Processes
The Processes tab of the System Monitor application.

Figure 19.1. System Monitor - Processes


For each listed process, the System Monitor tool displays its name (Process Name), current status (Status), percentage of the CPU usage (% CPU), nice value (Nice), process ID (ID), memory usage (Memory), the channel the process is waiting in (Waiting Channel), and additional details about the session (Session). To sort the information by a specific column in ascending order, click the name of that column. Click the name of the column again to toggle the sort between ascending and descending order.
By default, the System Monitor tool displays a list of processes that are owned by the current user. Selecting various options from the View menu allows you to:
  • view only active processes,
  • view all processes,
  • view your processes,
  • view process dependencies,
  • view a memory map of a selected process,
  • view the files opened by a selected process, and
  • refresh the list of processes.
Additionally, various options in the Edit menu allows you to:
  • stop a process,
  • continue running a stopped process,
  • end a process,
  • kill a process,
  • change the priority of a selected process, and
  • edit the System Monitor preferences, such as the refresh interval for the list of processes, or what information to show.
You can also end a process by selecting it from the list and clicking the End Process button.

19.2. Viewing Memory Usage

19.2.1. Using the free Command

The free command allows you to display the amount of free and used memory on the system. To do so, type the following at a shell prompt:
free
The free command provides information about both the physical memory (Mem) and swap space (Swap). It displays the total amount of memory (total), as well as the amount of memory that is in use (used), free (free), shared (shared), in kernel buffers (buffers), and cached (cached). For example:
~]$ free total   used   free shared buffers cachedMem: 760752 661332  99420  0   6476 317200-/+ buffers/cache: 337656 423096Swap:  1540088 283652 1256436
By default, free displays the values in kilobytes. To display the values in megabytes, supply the -m command line option:
free -m
For instance:
~]$ free -m total   used   free shared buffers cachedMem:   742 646 96  0  6 309-/+ buffers/cache: 330 412Swap: 1503 276   1227
For a complete list of available command line options, refer to the free(1) manual page.

19.2.2. Using the System Monitor Tool

The Resources tab of the System Monitor tool allows you to view the amount of free and used memory on the system.
To start the System Monitor tool, either select ApplicationsSystem ToolsSystem Monitor from the panel, or type gnome-system-monitor at a shell prompt. Then click the Resources tab to view the system's memory usage.
System Monitor - Resources
The Resources tab of the System Monitor application.

Figure 19.2. System Monitor - Resources


In the Memory and Swap History section, the System Monitor tool displays a graphical representation of the memory and swap usage history, as well as the total amount of the physical memory (Memory) and swap space (Swap) and how much of it is in use.

19.3. Viewing CPU Usage

19.3.1. Using the System Monitor Tool

The Resources tab of the System Monitor tool allows you to view the current CPU usage on the system.
To start the System Monitor tool, either select ApplicationsSystem ToolsSystem Monitor from the panel, or type gnome-system-monitor at a shell prompt. Then click the Resources tab to view the system's CPU usage.
System Monitor - Resources
The Resources tab of the System Monitor application.

Figure 19.3. System Monitor - Resources


In the CPU History section, the System Monitor tool displays a graphical representation of the CPU usage history and shows the percentage of how much CPU is currently in use.

19.4. Viewing Block Devices and File Systems

19.4.1. Using the lsblk Command

The lsblk command allows you to display a list of available block devices. To do so, type the following at a shell prompt:
lsblk
For each listed block device, the lsblk command displays the device name (NAME), major and minor device number (MAJ:MIN), if the device is removable (RM), what is its size (SIZE), if the device is read-only (RO), what type is it (TYPE), and where the device is mounted (MOUNTPOINT). For example:
~]$ lsblkNAME  MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsr0 11:0 1  1024M  0 romvda   252:0 0 20G  0 rom|-vda1 252:1 0   500M  0 part /boot`-vda2 252:2 0  19.5G  0 part  |-vg_kvm-lv_root (dm-0) 253:0 0 18G  0 lvm  /  `-vg_kvm-lv_swap (dm-1) 253:1 0   1.5G  0 lvm  [SWAP]
By default, lsblk lists block devices in a tree-like format. To display the information as an ordinary list, add the -l command line option:
lsblk -l
For instance:
~]$ lsblk -lNAME  MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsr0 11:0 1  1024M  0 romvda   252:0 0 20G  0 romvda1  252:1 0   500M  0 part /bootvda2  252:2 0  19.5G  0 partvg_kvm-lv_root (dm-0) 253:0 0 18G  0 lvm  /vg_kvm-lv_swap (dm-1) 253:1 0   1.5G  0 lvm  [SWAP]
For a complete list of available command line options, refer to the lsblk(8) manual page.

19.4.2. Using the blkid Command

The blkid command allows you to display information about available block devices. To do so, type the following at a shell prompt as root:
blkid
For each listed block device, the blkid command displays available attributes such as its universally unique identifier (UUID), file system type (TYPE), or volume label (LABEL). For example:
~]# blkid/dev/vda1: UUID="7fa9c421-0054-4555-b0ca-b470a97a3d84" TYPE="ext4"/dev/vda2: UUID="7IvYzk-TnnK-oPjf-ipdD-cofz-DXaJ-gPdgBW" TYPE="LVM2_member"/dev/mapper/vg_kvm-lv_root: UUID="a07b967c-71a0-4925-ab02-aebcad2ae824" TYPE="ext4"/dev/mapper/vg_kvm-lv_swap: UUID="d7ef54ca-9c41-4de4-ac1b-4193b0c1ddb6" TYPE="swap"
By default, the lsblk command lists all available block devices. To display information about a particular device only, specify the device name on the command line:
blkid device_name
For instance, to display information about /dev/vda1, type:
~]# blkid /dev/vda1/dev/vda1: UUID="7fa9c421-0054-4555-b0ca-b470a97a3d84" TYPE="ext4"
You can also use the above command with the -p and -o udev command line options to obtain more detailed information. Note that root privileges are required to run this command:
blkid -po udev device_name
For example:
~]# blkid -po udev /dev/vda1ID_FS_UUID=7fa9c421-0054-4555-b0ca-b470a97a3d84ID_FS_UUID_ENC=7fa9c421-0054-4555-b0ca-b470a97a3d84ID_FS_VERSION=1.0ID_FS_TYPE=ext4ID_FS_USAGE=filesystem
For a complete list of available command line options, refer to the blkid(8) manual page.

19.4.3. Using the findmnt Command

The findmnt command allows you to display a list of currently mounted file systems. To do so, type the following at a shell prompt:
findmnt
For each listed file system, the findmnt command displays the target mount point (TARGET), source device (SOURCE), file system type (FSTYPE), and relevant mount options (OPTIONS). For example:
~]$ findmntTARGET   SOURCE FSTYPE   OPTIONS/ /dev/mapper/vg_kvm-lv_root ext4 rw,relatime,sec|-/proc  /proc  proc rw,relatime| |-/proc/bus/usb /proc/bus/usb  usbfs rw,relatime| `-/proc/sys/fs/binfmt_misc binfmt_m rw,relatime|-/sys   /sys   sysfs rw,relatime,sec|-/selinux  selinuxf rw,relatime|-/dev   udev   devtmpfs rw,relatime,sec| `-/dev udev   devtmpfs rw,relatime,sec|   |-/dev/pts   devpts devpts   rw,relatime,sec|   `-/dev/shm   tmpfs  tmpfs rw,relatime,sec|-/boot  /dev/vda1  ext4 rw,relatime,sec|-/var/lib/nfs/rpc_pipefs sunrpc rpc_pipe rw,relatime|-/misc  /etc/auto.misc autofs   rw,relatime,fd=`-/net   -hosts autofs   rw,relatime,fd=[output truncated]
By default, findmnt lists file systems in a tree-like format. To display the information as an ordinary list, add the -l command line option:
findmnt -l
For instance:
~]$ findmnt -lTARGET   SOURCE FSTYPE   OPTIONS/proc /proc  proc rw,relatime/sys /sys   sysfs rw,relatime,seclabe/dev udev   devtmpfs rw,relatime,seclabe/dev/pts devpts devpts   rw,relatime,seclabe/dev/shm tmpfs  tmpfs rw,relatime,seclabe/ /dev/mapper/vg_kvm-lv_root ext4 rw,relatime,seclabe/selinux selinuxf rw,relatime/dev udev   devtmpfs rw,relatime,seclabe/proc/bus/usb /proc/bus/usb  usbfs rw,relatime/boot /dev/vda1  ext4 rw,relatime,seclabe/proc/sys/fs/binfmt_misc binfmt_m rw,relatime/var/lib/nfs/rpc_pipefs  sunrpc rpc_pipe rw,relatime/misc /etc/auto.misc autofs   rw,relatime,fd=7,pg/net -hosts autofs   rw,relatime,fd=13,p[output truncated]
You can also choose to list only file systems of a particular type. To do so, add the -t command line option followed by a file system type:
findmnt -t type
For example, to all list ext4 file systems, type:
~]$ findmnt -t ext4TARGET SOURCE FSTYPE OPTIONS/  /dev/mapper/vg_kvm-lv_root ext4   rw,relatime,seclabel,barrier=1,data=ord/boot  /dev/vda1  ext4   rw,relatime,seclabel,barrier=1,data=ord
For a complete list of available command line options, refer to the findmnt(8) manual page.

19.4.4. Using the df Command

The df command allows you to display a detailed report on the system's disk space usage. To do so, type the following at a shell prompt:
df
For each listed file system, the df command displays its name (Filesystem), size (1K-blocks or Size), how much space is used (Used), how much space is still available (Available), the percentage of space usage (Use%), and where is the file system mounted (Mounted on). For example:
~]$ dfFilesystem 1K-blocks  Used Available Use% Mounted on/dev/mapper/vg_kvm-lv_root  18618236   4357360  13315112  25% /tmpfs 380376   288 380088   1% /dev/shm/dev/vda1 495844 77029 393215  17% /boot
By default, the df command shows the partition size in 1 kilobyte blocks and the amount of used and available disk space in kilobytes. To view the information in megabytes and gigabytes, supply the -h command line option, which causes df to display the values in a human-readable format:
df -h
For instance:
~]$ df -hFilesystem  Size  Used Avail Use% Mounted on/dev/mapper/vg_kvm-lv_root   18G  4.2G   13G  25% /tmpfs   372M  288K  372M   1% /dev/shm/dev/vda1   485M   76M  384M  17% /boot
For a complete list of available command line options, refer to the df(1) manual page.

19.4.5. Using the du Command

The du command allows you to displays the amount of space that is being used by files in a directory. To display the disk usage for each of the subdirectories in the current working directory, run the command with no additional command line options:
du
For example:
~]$ du14972   ./Downloads4   ./.gnome24   ./.mozilla/extensions4   ./.mozilla/plugins12  ./.mozilla15004   .
By default, the du command displays the disk usage in kilobytes. To view the information in megabytes and gigabytes, supply the -h command line option, which causes the utility to display the values in a human-readable format:
du -h
For instance:
~]$ du -h15M ./Downloads4.0K ./.gnome24.0K ./.mozilla/extensions4.0K ./.mozilla/plugins12K ./.mozilla15M .
At the end of the list, the du command always shows the grand total for the current directory. To display only this information, supply the -s command line option:
du -sh
For example:
~]$ du -sh15M .
For a complete list of available command line options, refer to the du(1) manual page.

19.4.6. Using the System Monitor Tool

The File Systems tab of the System Monitor tool allows you to view file systems and disk space usage in the graphical user interface.
To start the System Monitor tool, either select ApplicationsSystem ToolsSystem Monitor from the panel, or type gnome-system-monitor at a shell prompt. Then click the File Systems tab to view a list of file systems.
System Monitor - File Systems
The File Systems tab of the System Monitor application.

Figure 19.4. System Monitor - File Systems


For each listed file system, the System Monitor tool displays the source device (Device), target mount point (Directory), and file system type (Type), as well as its size (Total) and how much space is free (Free), available (Available), and used (Used).

19.5. Viewing Hardware Information

19.5.1. Using the lspci Command

The lspci command allows you to display information about PCI buses and devices that are attached to them. To list all PCI devices that are in the system, type the following at a shell prompt:
lspci
This displays a simple list of devices, for example:
~]$ lspci00:00.0 Host bridge: Intel Corporation 82X38/X48 Express DRAM Controller00:01.0 PCI bridge: Intel Corporation 82X38/X48 Express Host-Primary PCI Express Bridge00:1a.0 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #4 (rev 02)00:1a.1 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #5 (rev 02)00:1a.2 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #6 (rev 02)[output truncated]
You can also use the -v command line option to display more verbose output, or -vv for very verbose output:
lspci -v|-vv
For instance, to determine the manufacturer, model, and memory size of a system's video card, type:
~]$ lspci -v[output truncated]01:00.0 VGA compatible controller: nVidia Corporation G84 [Quadro FX 370] (rev a1) (prog-if 00 [VGA controller]) Subsystem: nVidia Corporation Device 0491 Physical Slot: 2 Flags: bus master, fast devsel, latency 0, IRQ 16 Memory at f2000000 (32-bit, non-prefetchable) [size=16M] Memory at e0000000 (64-bit, prefetchable) [size=256M] Memory at f0000000 (64-bit, non-prefetchable) [size=32M] I/O ports at 1100 [size=128] Expansion ROM at <unassigned> [disabled] Capabilities: <access denied> Kernel driver in use: nouveau Kernel modules: nouveau, nvidiafb[output truncated]
For a complete list of available command line options, refer to the lspci(8) manual page.

19.5.2. Using the lsusb Command

The lsusb command allows you to display information about USB buses and devices that are attached to them. To list all USB devices that are in the system, type the following at a shell prompt:
lsusb
This displays a simple list of devices, for example:
~]$ lsusbBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub[output truncated]Bus 001 Device 002: ID 0bda:0151 Realtek Semiconductor Corp. Mass Storage Device (Multicard Reader)Bus 008 Device 002: ID 03f0:2c24 Hewlett-Packard Logitech M-UAL-96 MouseBus 008 Device 003: ID 04b3:3025 IBM Corp.
You can also use the -v command line option to display more verbose output:
lsusb -v
For instance:
~]$ lsusb -v[output truncated]Bus 008 Device 002: ID 03f0:2c24 Hewlett-Packard Logitech M-UAL-96 MouseDevice Descriptor:  bLength 18  bDescriptorType 1  bcdUSB   2.00  bDeviceClass 0 (Defined at Interface level)  bDeviceSubClass 0  bDeviceProtocol 0  bMaxPacketSize0 8  idVendor   0x03f0 Hewlett-Packard  idProduct  0x2c24 Logitech M-UAL-96 Mouse  bcdDevice   31.00  iManufacturer   1  iProduct 2  iSerial 0  bNumConfigurations  1  Configuration Descriptor: bLength 9 bDescriptorType 2[output truncated]
For a complete list of available command line options, refer to the lsusb(8) manual page.

19.5.3. Using the lspcmcia Command

The lspcmcia command allows you to list all PCMCIA devices that are present in the system. To do so, type the following at a shell prompt:
lspcmcia
For example:
~]$ lspcmciaSocket 0 Bridge: [yenta_cardbus] (bus ID: 0000:15:00.0)
You can also use the -v command line option to display more verbose information, or -vv to increase the verbosity level even further:
lspcmcia -v|-vv
For instance:
~]$ lspcmcia -vSocket 0 Bridge: [yenta_cardbus] (bus ID: 0000:15:00.0) Configuration:  state: on   ready: unknown
For a complete list of available command line options, refer to the pccardctl(8) manual page.

19.5.4. Using the lscpu Command

The lscpu command allows you to list information about CPUs that are present in the system, including the number of CPUs, their architecture, vendor, family, model, CPU caches, etc. To do so, type the following at a shell prompt:
lscpu
For example:
~]$ lscpuArchitecture:  x86_64CPU op-mode(s): 32-bit, 64-bitByte Order: Little EndianCPU(s): 4On-line CPU(s) list:   0-3Thread(s) per core: 1Core(s) per socket: 4Socket(s): 1NUMA node(s):  1Vendor ID: GenuineIntelCPU family: 6Model: 23Stepping:  7CPU MHz:   1998.000BogoMIPS:  4999.98Virtualization: VT-xL1d cache: 32KL1i cache: 32KL2 cache:  3072KNUMA node0 CPU(s): 0-3
For a complete list of available command line options, refer to the lscpu(1) manual page.

19.6. Monitoring Performance with Net-SNMP

Red Hat Enterprise Linux 6 includes the Net-SNMP software suite, which includes a flexible and extensible Simple Network Management Protocol (SNMP) agent. This agent and its associated utilities can be used to provide performance data from a large number of systems to a variety of tools which support polling over the SNMP protocol.
This section provides information on configuring the Net-SNMP agent to securely provide performance data over the network, retrieving the data using the SNMP protocol, and extending the SNMP agent to provide custom performance metrics.

19.6.1. Installing Net-SNMP

The Net-SNMP software suite is available as a set of RPM packages in the Red Hat Enterprise Linux software distribution. Table 19.2, "Available Net-SNMP packages" summarizes each of the packages and their contents.

Table 19.2. Available Net-SNMP packages

PackageProvides
net-snmpThe SNMP Agent Daemon and documentation. This package is required for exporting performance data.
net-snmp-libsThe netsnmp library and the bundled management information bases (MIBs). This package is required for exporting performance data.
net-snmp-utilsSNMP clients such as snmpget and snmpwalk. This package is required in order to query a system's performance data over SNMP.
net-snmp-perlThe mib2c utility and the NetSNMP Perl module.
net-snmp-pythonAn SNMP client library for Python.

To install any of these packages, use the yum command in the following form:
yum install package . . . . . . 
For example, to install the SNMP Agent Daemon and SNMP clients used in the rest of this section, type the following at a shell prompt:
~]# yum install net-snmp net-snmp-libs net-snmp-utils
Note that you must have superuser privileges (that is, you must be logged in as root) to run this command. For more information on how to install new packages in Red Hat Enterprise Linux, refer to Section 6.2.4, "Installing Packages".

19.6.2. Running the Net-SNMP Daemon

The net-snmp package contains snmpd, the SNMP Agent Daemon. This section provides information on how to start, stop, and restart the snmpd service, and shows how to enable it in a particular runlevel. For more information on the concept of runlevels and how to manage system services in Red Hat Enterprise Linux in general, refer to Chapter 10, Services and Daemons.

19.6.2.1. Starting the Service

To run the snmpd service in the current session, type the following at a shell prompt as root:
service snmpd start
To configure the service to be automatically started at boot time, use the following command:
chkconfig snmpd on
This will enable the service in runlevel 2, 3, 4, and 5. Alternatively, you can use the Service Configuration utility as described in Section 10.2.1.1, "Enabling and Disabling a Service".

19.6.2.2. Stopping the Service

To stop the running snmpd service, type the following at a shell prompt as root:
service snmpd stop
To disable starting the service at boot time, use the following command:
chkconfig snmpd off
This will disable the service in all runlevels. Alternatively, you can use the Service Configuration utility as described in Section 10.2.1.1, "Enabling and Disabling a Service".

19.6.2.3. Restarting the Service

To restart the running snmpd service, type the following at a shell prompt:
service snmpd restart
This will stop the service and start it again in quick succession. To only reload the configuration without stopping the service, run the following command instead:
service snmpd reload
This will cause the running snmpd service to reload the configuration.
Alternatively, you can use the Service Configuration utility as described in Section 10.2.1.2, "Starting, Restarting, and Stopping a Service".

19.6.3. Configuring Net-SNMP

To change the Net-SNMP Agent Daemon configuration, edit the /etc/snmp/snmpd.conf configuration file. The default snmpd.conf file shipped with Red Hat Enterprise Linux 6 is heavily commented and serves as a good starting point for agent configuration.
This section focuses on two common tasks: setting system information and configuring authentication. For more information about available configuration directives, refer to the snmpd.conf(5) manual page. Additionally, there is a utility in the net-snmp package named snmpconf which can be used to interactively generate a valid agent configuration.
Note that the net-snmp-utils package must be installed in order to use the snmpwalk utility described in this section.

Applying the changes

For any changes to the configuration file to take effect, force the snmpd service to re-read the configuration by running the following command as root:
service snmpd reload

19.6.3.1. Setting System Information

Net-SNMP provides some rudimentary system information via the system tree. For example, the following snmpwalk command shows the system tree with a default agent configuration.
~]# snmpwalk -v2c -c public localhost systemSNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.32-122.el6.x86_64 #1 SMP Wed Mar 9 23:54:34 EST 2011 x86_64SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (99554) 0:16:35.54SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)SNMPv2-MIB::sysName.0 = STRING: localhost.localdomainSNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
By default, the sysName object is set to the hostname. The sysLocation and sysContact objects can be configured in the /etc/snmp/snmpd.conf file by changing the value of the syslocation and syscontact directives, for example:
syslocation Datacenter, Row 3, Rack 2syscontact UNIX Admin <[email protected]>
After making changes to the configuration file, reload the configuration and test it by running the snmpwalk command again:
~]# service snmpd reloadReloading snmpd: [  OK  ]~]# snmpwalk -v2c -c public localhost systemSNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.32-122.el6.x86_64 #1 SMP Wed Mar 9 23:54:34 EST 2011 x86_64SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (158357) 0:26:23.57SNMPv2-MIB::sysContact.0 = STRING: UNIX Admin <[email protected]>SNMPv2-MIB::sysName.0 = STRING: localhost.localdomainSNMPv2-MIB::sysLocation.0 = STRING: Datacenter, Row 3, Rack 2

19.6.3.2. Configuring Authentication

The Net-SNMP Agent Daemon supports all three versions of the SNMP protocol. The first two versions (1 and 2c) provide for simple authentication using a community string. This string is a shared secret between the agent and any client utilities. The string is passed in clear text over the network however and is not considered secure. Version 3 of the SNMP protocol supports user authentication and message encryption using a variety of protocols. The Net-SNMP agent also supports tunneling over SSH, TLS authentication with X.509 certificates, and Kerberos authentication.
Configuring SNMP Version 2c Community
To configure an SNMP version 2c community, use either the rocommunity or rwcommunity directive in the /etc/snmp/snmpd.conf configuration file. The format of the directives is the following:
directive community [source [OID]]
. . . . . . where community is the community string to use, source is an IP address or subnet, and OID is the SNMP tree to provide access to. For example, the following directive provides read-only access to the system tree to a client using the community string "redhat" on the local machine:
rocommunity redhat 127.0.0.1 .1.3.6.1.2.1.1
To test the configuration, use the snmpwalk command with the -v and -c options.
~]# snmpwalk -v2c -c redhat localhost systemSNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.32-122.el6.x86_64 #1 SMP Wed Mar 9 23:54:34 EST 2011 x86_64SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (158357) 0:26:23.57SNMPv2-MIB::sysContact.0 = STRING: UNIX Admin <[email protected]>SNMPv2-MIB::sysName.0 = STRING: localhost.localdomainSNMPv2-MIB::sysLocation.0 = STRING: Datacenter, Row 3, Rack 2
Configuring SNMP Version 3 User
To configure an SNMP version 3 user, use the net-snmp-create-v3-user command. This command adds entries to the /var/lib/net-snmp/snmpd.conf and /etc/snmp/snmpd.conf files which create the user and grant access to the user. Note that the net-snmp-create-v3-user command may only be run when the agent is not running. The following example creates the "admin" user with the password "redhatsnmp":
~]# service snmpd stopStopping snmpd: [  OK  ]~]# net-snmp-create-v3-userEnter a SNMPv3 user name to create:adminEnter authentication pass-phrase:redhatsnmpEnter encryption pass-phrase:  [press return to reuse the authentication pass-phrase]adding the following line to /var/lib/net-snmp/snmpd.conf:   createUser admin MD5 "redhatsnmp" DESadding the following line to /etc/snmp/snmpd.conf:   rwuser admin~]# service snmpd startStarting snmpd: [  OK  ]
The rwuser directive (or rouser when the -ro command line option is supplied) that net-snmp-create-v3-user adds to /etc/snmp/snmpd.conf has a similar format to the rwcommunity and rocommunity directives:
directive user [noauth|auth|priv] [OID]
. . . . . . where user is a username and OID is the SNMP tree to provide access to. By default, the Net-SNMP Agent Daemon allows only authenticated requests (the auth option). The noauth option allows you to permit unauthenticated requests, and the priv option enforces the use of encryption. The authpriv option specifies that requests must be authenticated and replies should be encrypted.
For example, the following line grants the user "admin" read-write access to the entire tree:
rwuser admin authpriv .1
To test the configuration, create a .snmp directory in your user's home directory and a configuration file named snmp.conf in that directory (~/.snmp/snmp.conf) with the following lines:
defVersion 3defSecurityLevel authPrivdefSecurityName admindefPassphrase redhatsnmp
The snmpwalk command will now use these authentication settings when querying the agent:
~]$ snmpwalk -v3 localhost systemSNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.32-122.el6.x86_64 #1 SMP Wed Mar 9 23:54:34 EST 2011 x86_64[output truncated]

19.6.4. Retrieving Performance Data over SNMP

The Net-SNMP Agent in Red Hat Enterprise Linux provides a wide variety of performance information over the SNMP protocol. In addition, the agent can be queried for a listing of the installed RPM packages on the system, a listing of currently running processes on the system, or the network configuration of the system.
This section provides an overview of OIDs related to performance tuning available over SNMP. It assumes that the net-snmp-utils package is installed and that the user is granted access to the SNMP tree as described in Section 19.6.3.2, "Configuring Authentication".

19.6.4.1. Hardware Configuration

The Host Resources MIB included with Net-SNMP presents information about the current hardware and software configuration of a host to a client utility. Table 19.3, "Available OIDs" summarizes the different OIDs available under that MIB.

Table 19.3. Available OIDs

OIDDescription
HOST-RESOURCES-MIB::hrSystemContains general system information such as uptime, number of users, and number of running processes.
HOST-RESOURCES-MIB::hrStorageContains data on memory and file system usage.
HOST-RESOURCES-MIB::hrDevicesContains a listing of all processors, network devices, and file systems.
HOST-RESOURCES-MIB::hrSWRunContains a listing of all running processes.
HOST-RESOURCES-MIB::hrSWRunPerfContains memory and CPU statistics on the process table from HOST-RESOURCES-MIB::hrSWRun.
HOST-RESOURCES-MIB::hrSWInstalledContains a listing of the RPM database.

There are also a number of SNMP tables available in the Host Resources MIB which can be used to retrieve a summary of the available information. The following example displays HOST-RESOURCES-MIB::hrFSTable:
~]$ snmptable -Cb localhost HOST-RESOURCES-MIB::hrFSTableSNMP table: HOST-RESOURCES-MIB::hrFSTable Index MountPoint RemoteMountPoint Type Access Bootable StorageIndex LastFullBackupDate LastPartialBackupDate 1 "/"   "" HOST-RESOURCES-TYPES::hrFSLinuxExt2 readWrite true   31  0-1-1,0:0:0.0 0-1-1,0:0:0.0 5 "/dev/shm"   "" HOST-RESOURCES-TYPES::hrFSOther readWrite false   35  0-1-1,0:0:0.0 0-1-1,0:0:0.0 6 "/boot"   "" HOST-RESOURCES-TYPES::hrFSLinuxExt2 readWrite false   36  0-1-1,0:0:0.0 0-1-1,0:0:0.0
For more information about HOST-RESOURCES-MIB, see the /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt file.

19.6.4.2. CPU and Memory Information

Most system performance data is available in the UCD SNMP MIB. The systemStats OID provides a number of counters around processor usage:
~]$ snmpwalk localhost UCD-SNMP-MIB::systemStatsUCD-SNMP-MIB::ssIndex.0 = INTEGER: 1UCD-SNMP-MIB::ssErrorName.0 = STRING: systemStatsUCD-SNMP-MIB::ssSwapIn.0 = INTEGER: 0 kBUCD-SNMP-MIB::ssSwapOut.0 = INTEGER: 0 kBUCD-SNMP-MIB::ssIOSent.0 = INTEGER: 0 blocks/sUCD-SNMP-MIB::ssIOReceive.0 = INTEGER: 0 blocks/sUCD-SNMP-MIB::ssSysInterrupts.0 = INTEGER: 29 interrupts/sUCD-SNMP-MIB::ssSysContext.0 = INTEGER: 18 switches/sUCD-SNMP-MIB::ssCpuUser.0 = INTEGER: 0UCD-SNMP-MIB::ssCpuSystem.0 = INTEGER: 0UCD-SNMP-MIB::ssCpuIdle.0 = INTEGER: 99UCD-SNMP-MIB::ssCpuRawUser.0 = Counter32: 2278UCD-SNMP-MIB::ssCpuRawNice.0 = Counter32: 1395UCD-SNMP-MIB::ssCpuRawSystem.0 = Counter32: 6826UCD-SNMP-MIB::ssCpuRawIdle.0 = Counter32: 3383736UCD-SNMP-MIB::ssCpuRawWait.0 = Counter32: 7629UCD-SNMP-MIB::ssCpuRawKernel.0 = Counter32: 0UCD-SNMP-MIB::ssCpuRawInterrupt.0 = Counter32: 434UCD-SNMP-MIB::ssIORawSent.0 = Counter32: 266770UCD-SNMP-MIB::ssIORawReceived.0 = Counter32: 427302UCD-SNMP-MIB::ssRawInterrupts.0 = Counter32: 743442UCD-SNMP-MIB::ssRawContexts.0 = Counter32: 718557UCD-SNMP-MIB::ssCpuRawSoftIRQ.0 = Counter32: 128UCD-SNMP-MIB::ssRawSwapIn.0 = Counter32: 0UCD-SNMP-MIB::ssRawSwapOut.0 = Counter32: 0
In particular, the ssCpuRawUser, ssCpuRawSystem, ssCpuRawWait, and ssCpuRawIdle OIDs provide counters which are helpful when determining whether a system is spending most of its processor time in kernel space, user space, or I/O. ssRawSwapIn and ssRawSwapOut can be helpful when determining whether a system is suffering from memory exhaustion.
More memory information is available under the UCD-SNMP-MIB::memory OID, which provides similar data to the free command:
~]$ snmpwalk localhost UCD-SNMP-MIB::memoryUCD-SNMP-MIB::memIndex.0 = INTEGER: 0UCD-SNMP-MIB::memErrorName.0 = STRING: swapUCD-SNMP-MIB::memTotalSwap.0 = INTEGER: 1023992 kBUCD-SNMP-MIB::memAvailSwap.0 = INTEGER: 1023992 kBUCD-SNMP-MIB::memTotalReal.0 = INTEGER: 1021588 kBUCD-SNMP-MIB::memAvailReal.0 = INTEGER: 634260 kBUCD-SNMP-MIB::memTotalFree.0 = INTEGER: 1658252 kBUCD-SNMP-MIB::memMinimumSwap.0 = INTEGER: 16000 kBUCD-SNMP-MIB::memBuffer.0 = INTEGER: 30760 kBUCD-SNMP-MIB::memCached.0 = INTEGER: 216200 kBUCD-SNMP-MIB::memSwapError.0 = INTEGER: noError(0)UCD-SNMP-MIB::memSwapErrorMsg.0 = STRING:
Load averages are also available in the UCD SNMP MIB. The SNMP table UCD-SNMP-MIB::laTable has a listing of the 1, 5, and 15 minute load averages:
~]$ snmptable localhost UCD-SNMP-MIB::laTableSNMP table: UCD-SNMP-MIB::laTable laIndex laNames laLoad laConfig laLoadInt laLoadFloat laErrorFlag laErrMessage   1  Load-1   0.00 12.00 0 0.000000 noError   2  Load-5   0.00 12.00 0 0.000000 noError   3 Load-15   0.00 12.00 0 0.000000 noError

19.6.4.3. File System and Disk Information

The Host Resources MIB provides information on file system size and usage. Each file system (and also each memory pool) has an entry in the HOST-RESOURCES-MIB::hrStorageTable table:
~]$ snmptable -Cb localhost HOST-RESOURCES-MIB::hrStorageTableSNMP table: HOST-RESOURCES-MIB::hrStorageTable Index Type   DescrAllocationUnits Size   Used AllocationFailures 1   HOST-RESOURCES-TYPES::hrStorageRam Physical memory1024 Bytes 1021588 388064  ? 3 HOST-RESOURCES-TYPES::hrStorageVirtualMemory  Virtual memory1024 Bytes 2045580 388064  ? 6 HOST-RESOURCES-TYPES::hrStorageOther  Memory buffers1024 Bytes 1021588  31048  ? 7 HOST-RESOURCES-TYPES::hrStorageOther   Cached memory1024 Bytes  216604 216604  ? 10 HOST-RESOURCES-TYPES::hrStorageVirtualMemory  Swap space1024 Bytes 1023992  0  ? 31 HOST-RESOURCES-TYPES::hrStorageFixedDisk   /4096 Bytes 2277614 250391  ? 35 HOST-RESOURCES-TYPES::hrStorageFixedDisk /dev/shm4096 Bytes  127698  0  ? 36 HOST-RESOURCES-TYPES::hrStorageFixedDisk   /boot1024 Bytes  198337  26694  ?
The OIDs under HOST-RESOURCES-MIB::hrStorageSize and HOST-RESOURCES-MIB::hrStorageUsed can be used to calculate the remaining capacity of each mounted file system.
I/O data is available both in UCD-SNMP-MIB::systemStats (ssIORawSent.0 and ssIORawRecieved.0) and in UCD-DISKIO-MIB::diskIOTable. The latter provides much more granular data. Under this table are OIDs for diskIONReadX and diskIONWrittenX, which provide counters for the number of bytes read from and written to the block device in question since the system boot:
~]$ snmptable -Cb localhost UCD-DISKIO-MIB::diskIOTableSNMP table: UCD-DISKIO-MIB::diskIOTable Index Device NRead  NWritten Reads Writes LA1 LA5 LA15 NReadX NWrittenX... 25 sda 216886272 139109376 16409   4894   ?   ? ? 216886272 139109376 26   sda1   2455552  5120   613  2   ?   ? ?   2455552  5120 27   sda2   1486848 0   332  0   ?   ? ?   1486848 0 28   sda3 212321280 139104256 15312   4871   ?   ? ? 212321280 139104256

19.6.4.4. Network Information

The Interfaces MIB provides information on network devices. IF-MIB::ifTable provides an SNMP table with an entry for each interface on the system, the configuration of the interface, and various packet counters for the interface. The following example shows the first few columns of ifTable on a system with two physical network interfaces:
~]$ snmptable -Cb localhost IF-MIB::ifTableSNMP table: IF-MIB::ifTable Index Descr Type   Mtu Speed  PhysAddress AdminStatus 1 lo softwareLoopback 16436 10000000   up 2  eth0   ethernetCsmacd  1500 0 52:54:0:c7:69:58  up 3  eth1   ethernetCsmacd  1500 0 52:54:0:a7:a3:24 down
Network traffic is available under the OIDs IF-MIB::ifOutOctets and IF-MIB::ifInOctets. The following SNMP queries will retrieve network traffic for each of the interfaces on this system:
~]$ snmpwalk localhost IF-MIB::ifDescrIF-MIB::ifDescr.1 = STRING: loIF-MIB::ifDescr.2 = STRING: eth0IF-MIB::ifDescr.3 = STRING: eth1~]$ snmpwalk localhost IF-MIB::ifOutOctetsIF-MIB::ifOutOctets.1 = Counter32: 10060699IF-MIB::ifOutOctets.2 = Counter32: 650IF-MIB::ifOutOctets.3 = Counter32: 0~]$ snmpwalk localhost IF-MIB::ifInOctetsIF-MIB::ifInOctets.1 = Counter32: 10060699IF-MIB::ifInOctets.2 = Counter32: 78650IF-MIB::ifInOctets.3 = Counter32: 0

19.6.5. Extending Net-SNMP

The Net-SNMP Agent can be extended to provide application metrics in addition to raw system metrics. This allows for capacity planning as well as performance issue troubleshooting. For example, it may be helpful to know that an email system had a 5-minute load average of 15 while being tested, but it is more helpful to know that the email system has a load average of 15 while processing 80,000 messages a second. When application metrics are available via the same interface as the system metrics, this also allows for the visualization of the impact of different load scenarios on system performance (for example, each additional 10,000 messages increases the load average linearly until 100,000).
A number of the applications that ship with Red Hat Enterprise Linux extend the Net-SNMP Agent to provide application metrics over SNMP. There are several ways to extend the agent for custom applications as well. This section describes extending the agent with shell scripts and Perl plug-ins. It assumes that the net-snmp-utils and net-snmp-perl packages are installed, and that the user is granted access to the SNMP tree as described in Section 19.6.3.2, "Configuring Authentication".

19.6.5.1. Extending Net-SNMP with Shell Scripts

The Net-SNMP Agent provides an extension MIB (NET-SNMP-EXTEND-MIB) that can be used to query arbitrary shell scripts. To specify the shell script to run, use the extend directive in the /etc/snmp/snmpd.conf file. Once defined, the Agent will provide the exit code and any output of the command over SNMP. The example below demonstrates this mechanism with a script which determines the number of httpd processes in the process table.

Using the proc directive

The Net-SNMP Agent also provides a built-in mechanism for checking the process table via the proc directive. Refer to the snmpd.conf(5) manual page for more information.
The exit code of the following shell script is the number of httpd processes running on the system at a given point in time:
#!/bin/shNUMPIDS=`pgrep httpd | wc -l`exit $NUMPIDS
To make this script available over SNMP, copy the script to a location on the system path, set the executable bit, and add an extend directive to the /etc/snmp/snmpd.conf file. The format of the extend directive is the following:
extend name prog args
. . . . . . where name is an identifying string for the extension, prog is the program to run, and args are the arguments to give the program. For instance, if the above shell script is copied to /usr/local/bin/check_apache.sh, the following directive will add the script to the SNMP tree:
extend httpd_pids /bin/sh /usr/local/bin/check_apache.sh
The script can then be queried at NET-SNMP-EXTEND-MIB::nsExtendObjects:
~]$ snmpwalk localhost NET-SNMP-EXTEND-MIB::nsExtendObjectsNET-SNMP-EXTEND-MIB::nsExtendNumEntries.0 = INTEGER: 1NET-SNMP-EXTEND-MIB::nsExtendCommand."httpd_pids" = STRING: /bin/shNET-SNMP-EXTEND-MIB::nsExtendArgs."httpd_pids" = STRING: /usr/local/bin/check_apache.shNET-SNMP-EXTEND-MIB::nsExtendInput."httpd_pids" = STRING:NET-SNMP-EXTEND-MIB::nsExtendCacheTime."httpd_pids" = INTEGER: 5NET-SNMP-EXTEND-MIB::nsExtendExecType."httpd_pids" = INTEGER: exec(1)NET-SNMP-EXTEND-MIB::nsExtendRunType."httpd_pids" = INTEGER: run-on-read(1)NET-SNMP-EXTEND-MIB::nsExtendStorage."httpd_pids" = INTEGER: permanent(4)NET-SNMP-EXTEND-MIB::nsExtendStatus."httpd_pids" = INTEGER: active(1)NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."httpd_pids" = STRING:NET-SNMP-EXTEND-MIB::nsExtendOutputFull."httpd_pids" = STRING:NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."httpd_pids" = INTEGER: 1NET-SNMP-EXTEND-MIB::nsExtendResult."httpd_pids" = INTEGER: 8NET-SNMP-EXTEND-MIB::nsExtendOutLine."httpd_pids".1 = STRING:
Note that the exit code ("8" in this example) is provided as an INTEGER type and any output is provided as a STRING type. To expose multiple metrics as integers, supply different arguments to the script using the extend directive. For example, the following shell script can be used to determine the number of processes matching an arbitrary string, and will also output a text string giving the number of processes:
#!/bin/shPATTERN=$1NUMPIDS=`pgrep $PATTERN | wc -l`echo "There are $NUMPIDS $PATTERN processes."exit $NUMPIDS
The following /etc/snmp/snmpd.conf directives will give both the number of httpd PIDs as well as the number of snmpd PIDs when the above script is copied to /usr/local/bin/check_proc.sh:
extend httpd_pids /bin/sh /usr/local/bin/check_proc.sh httpdextend snmpd_pids /bin/sh /usr/local/bin/check_proc.sh snmpd
The following example shows the output of an snmpwalk of the nsExtendObjects OID:
~]$ snmpwalk localhost NET-SNMP-EXTEND-MIB::nsExtendObjectsNET-SNMP-EXTEND-MIB::nsExtendNumEntries.0 = INTEGER: 2NET-SNMP-EXTEND-MIB::nsExtendCommand."httpd_pids" = STRING: /bin/shNET-SNMP-EXTEND-MIB::nsExtendCommand."snmpd_pids" = STRING: /bin/shNET-SNMP-EXTEND-MIB::nsExtendArgs."httpd_pids" = STRING: /usr/local/bin/check_proc.sh httpdNET-SNMP-EXTEND-MIB::nsExtendArgs."snmpd_pids" = STRING: /usr/local/bin/check_proc.sh snmpdNET-SNMP-EXTEND-MIB::nsExtendInput."httpd_pids" = STRING:NET-SNMP-EXTEND-MIB::nsExtendInput."snmpd_pids" = STRING:...NET-SNMP-EXTEND-MIB::nsExtendResult."httpd_pids" = INTEGER: 8NET-SNMP-EXTEND-MIB::nsExtendResult."snmpd_pids" = INTEGER: 1NET-SNMP-EXTEND-MIB::nsExtendOutLine."httpd_pids".1 = STRING: There are 8 httpd processes.NET-SNMP-EXTEND-MIB::nsExtendOutLine."snmpd_pids".1 = STRING: There are 1 snmpd processes.

Integer exit codes are limited

Integer exit codes are limited to a range of 0-255. For values that are likely to exceed 256, either use the standard output of the script (which will be typed as a string) or a different method of extending the agent.
This last example shows a query for the free memory of the system and the number of httpd processes. This query could be used during a performance test to determine the impact of the number of processes on memory pressure:
~]$ snmpget localhost \ 'NET-SNMP-EXTEND-MIB::nsExtendResult."httpd_pids"' \ UCD-SNMP-MIB::memAvailReal.0NET-SNMP-EXTEND-MIB::nsExtendResult."httpd_pids" = INTEGER: 8UCD-SNMP-MIB::memAvailReal.0 = INTEGER: 799664 kB

19.6.5.2. Extending Net-SNMP with Perl

Executing shell scripts using the extend directive is a fairly limited method for exposing custom application metrics over SNMP. The Net-SNMP Agent also provides an embedded Perl interface for exposing custom objects. The net-snmp-perl package provides the NetSNMP::agent Perl module that is used to write embedded Perl plug-ins on Red Hat Enterprise Linux.
The NetSNMP::agent Perl module provides an agent object which is used to handle requests for a part of the agent's OID tree. The agent object's constructor has options for running the agent as a sub-agent of snmpd or a standalone agent. No arguments are necessary to create an embedded agent:
use NetSNMP::agent (':all');my $agent = new NetSNMP::agent();
The agent object has a register method which is used to register a callback function with a particular OID. The register function takes a name, OID, and pointer to the callback function. The following example will register a callback function named hello_handler with the SNMP Agent which will handle requests under the OID .1.3.6.1.4.1.8072.9999.9999:
$agent->register("hello_world", ".1.3.6.1.4.1.8072.9999.9999", \&hello_handler);

Obtaining a root OID

The OID .1.3.6.1.4.1.8072.9999.9999 (NET-SNMP-MIB::netSnmpPlaypen) is typically used for demonstration purposes only. If your organization does not already have a root OID, you can obtain one by contacting an ISO Name Registration Authority (ANSI in the United States).
The handler function will be called with four parameters, HANDLER, REGISTRATION_INFO, REQUEST_INFO, and REQUESTS. The REQUESTS parameter contains a list of requests in the current call and should be iterated over and populated with data. The request objects in the list have get and set methods which allow for manipulating the OID and value of the request. For example, the following call will set the value of a request object to the string "hello world":
$request->setValue(ASN_OCTET_STR, "hello world");
The handler function should respond to two types of SNMP requests: the GET request and the GETNEXT request. The type of request is determined by calling the getMode method on the request_info object passed as the third parameter to the handler function. If the request is a GET request, the caller will expect the handler to set the value of the request object, depending on the OID of the request. If the request is a GETNEXT request, the caller will also expect the handler to set the OID of the request to the next available OID in the tree. This is illustrated in the following code example:
my $request;my $string_value = "hello world";my $integer_value = "8675309";for($request = $requests; $request; $request = $request->next()) {  my $oid = $request->getOID();  if ($request_info->getMode() == MODE_GET) { if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) {  $request->setValue(ASN_OCTET_STR, $string_value); } elsif ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.1")) {  $request->setValue(ASN_INTEGER, $integer_value); }  } elsif ($request_info->getMode() == MODE_GETNEXT) { if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) {  $request->setOID(".1.3.6.1.4.1.8072.9999.9999.1.1");  $request->setValue(ASN_INTEGER, $integer_value); } elsif ($oid < new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) {  $request->setOID(".1.3.6.1.4.1.8072.9999.9999.1.0");  $request->setValue(ASN_OCTET_STR, $string_value); }  }}
When getMode returns MODE_GET, the handler analyzes the value of the getOID call on the request object. The value of the request is set to either string_value if the OID ends in ".1.0", or set to integer_value if the OID ends in ".1.1". If the getMode returns MODE_GETNEXT, the handler determines whether the OID of the request is ".1.0", and then sets the OID and value for ".1.1". If the request is higher on the tree than ".1.0", the OID and value for ".1.0" is set. This in effect returns the "next" value in the tree so that a program like snmpwalk can traverse the tree without prior knowledge of the structure.
The type of the variable is set using constants from NetSNMP::ASN. See the perldoc for NetSNMP::ASN for a full list of available constants.
The entire code listing for this example Perl plug-in is as follows:
#!/usr/bin/perluse NetSNMP::agent (':all');use NetSNMP::ASN qw(ASN_OCTET_STR ASN_INTEGER);sub hello_handler {  my ($handler, $registration_info, $request_info, $requests) = @_;  my $request;  my $string_value = "hello world";  my $integer_value = "8675309";  for($request = $requests; $request; $request = $request->next()) { my $oid = $request->getOID(); if ($request_info->getMode() == MODE_GET) {  if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) { $request->setValue(ASN_OCTET_STR, $string_value);  }  elsif ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.1")) { $request->setValue(ASN_INTEGER, $integer_value);  } } elsif ($request_info->getMode() == MODE_GETNEXT) {  if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) { $request->setOID(".1.3.6.1.4.1.8072.9999.9999.1.1"); $request->setValue(ASN_INTEGER, $integer_value);  }  elsif ($oid < new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.1.0")) { $request->setOID(".1.3.6.1.4.1.8072.9999.9999.1.0"); $request->setValue(ASN_OCTET_STR, $string_value);  } }  }}my $agent = new NetSNMP::agent();$agent->register("hello_world", ".1.3.6.1.4.1.8072.9999.9999", \&hello_handler);
To test the plug-in, copy the above program to /usr/share/snmp/hello_world.pl and add the following line to the /etc/snmp/snmpd.conf configuration file:
perl do "/usr/share/snmp/hello_world.pl"
The SNMP Agent Daemon will need to be restarted to load the new Perl plug-in. Once it has been restarted, an snmpwalk should return the new data:
~]$ snmpwalk localhost NET-SNMP-MIB::netSnmpPlaypenNET-SNMP-MIB::netSnmpPlaypen.1.0 = STRING: "hello world"NET-SNMP-MIB::netSnmpPlaypen.1.1 = INTEGER: 8675309
The snmpget should also be used to exercise the other mode of the handler:
~]$ snmpget localhost \ NET-SNMP-MIB::netSnmpPlaypen.1.0 \ NET-SNMP-MIB::netSnmpPlaypen.1.1NET-SNMP-MIB::netSnmpPlaypen.1.0 = STRING: "hello world"NET-SNMP-MIB::netSnmpPlaypen.1.1 = INTEGER: 8675309

19.7. Additional Resources

To learn more about gathering system information, refer to the following resources.

19.7.1. Installed Documentation

  • ps(1) - The manual page for the ps command.
  • top(1) - The manual page for the top command.
  • free(1) - The manual page for the free command.
  • df(1) - The manual page for the df command.
  • du(1) - The manual page for the du command.
  • lspci(8) - The manual page for the lspci command.
  • snmpd(8) - The manual page for the snmpd service.
  • snmpd.conf(5) - The manual page for the /etc/snmp/snmpd.conf file containing full documentation of available configuration directives.
(Sebelumnya) 13 : Part VI. Monitoring and A ...13 : Chapter 20. Viewing and M ... (Berikutnya)