Thursday, December 24, 2009

Linux Tips and Tricks


To find out what a particular driver/module does you can usually use the modinfo command:
# modinfo -d e1000
Intel® PRO/1000 Network Driver
#
# modinfo -d hangcheck-timer
Hangcheck-timer detects when the system has gone out to lunch past a certain margin.
#



To get a list of parameters supported by a kernel driver/module, the modinfo command will usually provide the information:
# modinfo -p e1000
debug:Debug level (0=none,...,16=all)
InterruptThrottleRate:Interrupt Throttling Rate
RxAbsIntDelay:Receive Absolute Interrupt Delay
RxIntDelay:Receive Interrupt Delay
TxAbsIntDelay:Transmit Absolute Interrupt Delay
TxIntDelay:Transmit Interrupt Delay
XsumRX:Disable or enable Receive Checksum offload
FlowControl:Flow Control setting
AutoNeg:Advertised auto-negotiation setting
Duplex:Duplex setting
Speed:Speed setting
RxDescriptors:Number of receive descriptors
TxDescriptors:Number of transmit descriptors
#
# modinfo -p hangcheck-timer
hangcheck_dump_tasks:If nonzero, the machine will dump the system task state when the timer margin is exceeded.
hangcheck_reboot:If nonzero, the machine will reboot when the timer margin is exceeded.
hangcheck_margin:If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.
hangcheck_tick:Timer delay.
#

To set the parameters when modules are loaded, you can add entries to /etc/modprobe.conf on RHEL or /etc/modprobe.conf.local on SLES. For example:
options hangcheck-timer hangcheck_tick=30 hangcheck_margin=180
To load the module and see the new settings, run:
# modprobe -v hangcheck-timer
insmod /lib/modules/2.6.9-22.EL/kernel/drivers/char/hangcheck-timer.ko hangcheck_tick=20 hangcheck_margin=280
#
# dmesg | tail -1
Hangcheck: starting hangcheck timer 0.5.0 (tick is 30 seconds, margin is 180 seconds).
#
The newly loaded module will be at the top of the lsmod list:
# lsmod | head -2
Module Size Used by
hangcheck_timer 3289 0
#
To unload the module, run:
# rmmod hangcheck-timer
Posted by Werner in Linux Tips and Tricks at 21:07 | Comments (0) | Trackbacks (0)
Thursday, August 10. 2006
Renaming Files
You want to rename all files in a directory by adding a new extension. Try to use the xargs command:
ls | xargs -t -i mv {} {}.old
xargs reads each item from the ls ouput and executes the mv command. The ‘-i’ option tells xargs to replace ‘{}’ with the name of each item. The ‘-t’ option instructs xargs to print the command before executing it.
To rename a subset of files, specify the file names with the ls command:
ls *.log | xargs -t -i mv {} {}.old
Or to add a current timestamp extension you may want to use the date command similar to this one:
ls *.log | xargs -t -i mv {} {}.`date +%F-%H:%M:%S`
The extension will look like “.2006-08-10-19:37:16″.
If you want to rename the extension of files, try the rename command:
rename .log .log_archive.`date +%F-%H:%M:%S` *
This command replaces the first occurrence of ‘.log’ in the name by .log_archive.`date +%F-%H:%M:%S`.
The following command replaces .htm extensions with .html for all files that start with project*
rename .htm .html project*
Posted by Werner in Linux Tips and Tricks at 19:40 | Comments (0) | Trackbacks (0)
Thursday, August 3. 2006
Simple Network Performance Test
If you want to do a simple and quick network performance test you can use the ftp command.
FTP on Linux and other Unix systems allows you to pass shell commands to the ftp client by using the pipe symbol ‘|’ as the first character of the file name. With this feature you can send a very large file to a remote host using /dev/zero as input and /dev/null as output.
Example:
ftp> put “|dd if=/dev/zero bs=1M count=100″ /dev/null
This command transfers a large file without involving the disk and without having to cache the file in memory. If you use a large file on a disk it might become a bottleneck. In this example, “|dd if=/dev/zero bs=1M count=100″ becomes the input file. Since a dd command without the “of=” paramater prints the content of the file to standard output (stdout), the ftp client can read the output and pass it on to the remote file which is /dev/null on the remote host.
Posted by Werner in Linux Tips and Tricks at 22:56 | Comments (0) | Trackbacks (0)
Tuesday, July 25. 2006
Retrieving Hardware Information
If you need information on system’s hardware like vendor, manufacturer, product, S/N, etc. you can use:
dmidecode
The dmidecode command reads the information from the system BIOS, see alsohttp://www.nongnu.org/dmidecode/.
There are a few other commands you might want to check out which list installed hardware components etc.:
lsdev
lshal
lspci
lsusb
lsscsi
Starting with the 2.6 kernel you can now get lots of information from /sys. For example, to get information on an Emulex HBA:
# ls /sys/class/scsi_host/host1/
board_mode lpfc_cr_delay lpfc_poll option_rom_version
board_online lpfc_drvr_version lpfc_poll_tmo portnum
cmd_per_lun lpfc_fcp_class lpfc_scan_down proc_name
ctlreg lpfc_fdmi_on lpfc_topology programtype
device lpfc_hba_queue_depth lpfc_use_adisc scan
fwrev lpfc_link_speed management_version serialnum
hdw lpfc_log_verbose mbox sg_tablesize
host_busy lpfc_lun_queue_depth modeldesc state
info lpfc_max_luns modelname uevent
lpfc_ack0 lpfc_multi_ring_support nport_evt_cnt unchecked_isa_dma
lpfc_cr_count lpfc_nodev_tmo num_discovered_ports unique_id
#
Posted by Werner in Linux Tips and Tricks at 20:30 | Comments (0) | Trackbacks (0)
Sunday, July 23. 2006
Memory Fragmentation
When a Linux system has been running for a while memory fragmentation can increase which depends heavily on the nature of the applications that are running on it. The more processes allocate and free memory, the quicker memory becomes fragmented. And the kernel may not always be able to defragment enough memory for a requested size on time. If that happens, applications may not be able to allocate larger contiguous chunks of memory even though there is enough free memory available. Starting with the 2.6 kernel, i.e. RHEL4 and SLES9, memory management has improved tremendously and memory fragmentation has become less of an issue.
To see memory fragmentation you can use the magic SysRq key. Simply execute the following command:
# echo m > /proc/sysrq-trigger
This command will dump current memory information to /var/log/messages. Here is an example from a RHEL3 32-bit system:
Jul 23 20:19:30 localhost kernel: 0*4kB 0*8kB 0*16kB 1*32kB 0*64kB 1*128kB 1*256kB 1*512kB 1*1024kB 0*2048kB 0*4096kB = 1952kB)
Jul 23 20:19:30 localhost kernel: 1395*4kB 355*8kB 209*16kB 15*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 12244kB)
Jul 23 20:19:31 localhost kernel: 1479*4kB 673*8kB 205*16kB 73*32kB 21*64kB 847*128kB 473*256kB 92*512kB 164*1024kB 64*2048kB 28*4096kB = 708564kB)

The first line shows DMA memory fragmentation. The second line shows Low Memory fragmentation and the third line shows High Memory fragmentation. The output shows memory fragmentation in the Low Memory area. But there are many large memory chunks available in the High Memory area, e.g. 28 4MB.
If memory information was not dumped to /var/log/messages, then SysRq was not enabled. You can enable SysRq by setting sysrq to 1:
# echo 1 > /proc/sys/kernel/sysrq
Starting with the 2.6 kernel, i.e. RHEL4 and SLES9, you don’t need SysRq to dump memory information. You can simply check /proc/buddyinfo for memory fragmentation.
Here is the output of a 64-bit server running the 2.6 kernel:
# cat /proc/buddyinfo
Node 0, zone DMA 5 4 3 4 3 2 1 0 1 1 2
Node 0, zone Normal 1046 527 128 36 17 5 26 40 13 16 94
# echo m > /proc/sysrq-trigger
# grep Normal /var/log/messages | tail -1
Jul 23 21:42:26 localhost kernel: Normal: 1046*4kB 529*8kB 129*16kB 36*32kB 17*64kB 5*128kB 26*256kB 40*512kB 13*1024kB 16*2048kB 94*4096kB = 471600kB
#
In this example I used SysRq again to show what each number in /proc/buddyinfo is referring to.
Posted by Werner in Linux Tips and Tricks at 19:48 | Comments (0) | Trackbacks (0)
Saturday, July 22. 2006
Debugging Scripts
A script that is executed by an application or another script is failing. Executing the script manually doesn’t provide more clues since it’s only failing when invoked by another program. Also, you don’t want to redirect the output of the script each time you execute it. And you want to see more debugging information like line numbers.
Adding the following lines at the beginning of the script will help:
export PS4=’$0.$LINENO+ ‘
exec > /tmp/script.log
exec 2>&1
set -x
Example:
$ cat test
#!/bin/bash
export PS4=’$0.$LINENO+ ‘
exec > /tmp/script.log
exec 2>&1
set -x
ls -ld /etc
ls -ld /boot
echo “This is a test”
$ ./test
$ cat /tmp/script.log
./test.6+ ls -ld /etc
drwxr-xr-x 83 root root 7512 2006-07-22 16:49 /etc
./test.7+ ls -ld /boot
drwxr-xr-x 5 root root 1960 2006-07-22 15:30 /boot
./test.8+ echo ‘This is a test’
This is a test
$
These lines will turn on debugging and all information will be redirected to the log file. So you won’t have to redirect the output each time you run the script, e.g. “./script > /tmp/script.log 2>&1″. In some cases you can’t do that if the script is invoked by an application.
The PS4 builtin shell variable describes the prompt seen in debug mode. The $0 variable stands for the name of the script file itself. $LINENO shows the current line number within the script. The exec command redirects I/O streams. The first exec command redirects stdout stream 1 to /tmp/script.log. 2>&1 redirects stderr stream 2 to stdout stream 1. And “set -x” enables debugging.

No comments: