Friday, March 2, 2018

UNIX: check CPU usage and disk usage

Script to check cpu usage

grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'

#!/bin/bash

limit=2
email_destination="whatever@gmail.com"
email_body="WARNING the CPU usage on server `hostname` exceeded the limit you set ($limit)%"
email_subject="WARNING cpu usage"

usage=`grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}' | bc`

if echo $usage $limit | awk '{exit $1>$2?0:1}'
then
  echo -e $email_body | mail -s "$email_subject" $email_destination
  #echo 'alarm'
fi
_____________________________________________________

Disk space not updating in linux df -h after deleted files

ls -ld /proc/*/fd/* 2>&1 | fgrep '(deleted)'


restart the process that hold it

UNIX: sum and print total content of file

$ cat tpsCCRT.txt | awk '{sum+=$1} END {print "Count: ",NR;print "Avg :",sum/NR}'
Count:  1441
Avg : 15.6232
$

LinuxL Find file with certain permission


[root]# ls -l `find . -type f ! -perm 0755`
-rw-rw-r-- 1 root root    32550 Jan 18 14:20 ./centric_crm_tools-5.0.jar
-rw-rw-r-- 1 root root     8917 Jan 18 14:20 ./centric-troubleticketer-21.0.3.jar
-rw-rw-r-- 1 root root    10725 Jan 18 13:50 ./jdhcp-1.2.0.jar
-rw-rw-r-- 1 root root   228516 Jan 18 13:41 ./jradius-core-1.1.5.jar
-rw-rw-r-- 1 root root  4788404 Jan 18 13:41 ./jradius-dictionary-1.1.5.jar
-rw-r--r-- 1 root root    18289 Jan 18 13:51 ./opennms-alarm-northbounder-jms-21.0.3.jar
-rw-rw-r-- 1 root root    50911 Jan 18 13:51 ./opennms-dns-provisioning-adapter-21.0.3.jar
-rw-rw-r-- 1 root root    45429 Jan 18 13:49 ./opennms-integration-otrs-21.0.3.jar
-rw-rw-r-- 1 root root    60857 Jan 18 13:49 ./opennms-integration-otrs-31-21.0.3.jar
-rw-rw-r-- 1 root root     6037 Jan 18 13:49 ./opennms-integration-otrs-common-21.0.3.jar
-rw-rw-r-- 1 root root     7792 Jan 18 13:49 ./opennms-integration-rt-21.0.3.jar
-rw-r--r-- 1 root root 35841272 Jan 18 14:02 ./opennms_jmx_config_generator.jar
-rw-rw-r-- 1 root root   368488 Jan 18 13:51 ./opennms-rancid-provisioning-adapter-21.0.3.jar
-rw-rw-r-- 1 root root    49068 Jan 18 13:51 ./opennms-reverse-dns-provisioning-adapter-21.0.3.jar
-rw-rw-r-- 1 root root    54680 Jan 18 13:51 ./opennms-snmp-asset-provisioning-adapter-21.0.3.jar
-rw-rw-r-- 1 root root    55477 Jan 18 13:51 ./opennms-snmp-hardware-inventory-provisioning-adapter-21.0.3.jar
-rw-rw-r-- 1 root root    11951 Jan 18 13:51 ./opennms-vtdxml-collector-handler-21.0.3.jar
-rw-rw-r-- 1 root root  1871026 Jan 18 13:41 ./org.opennms.dependencies.jradius-extended-21.0.3.jar
-rw-rw-r-- 1 root root    22018 Jan 18 13:47 ./org.opennms.features.juniper-tca-collector-21.0.3.jar
-rw-r--r-- 1 root root    23005 Jan 18 13:50 ./org.opennms.features.ncs.ncs-drools-21.0.3.jar
-rw-r--r-- 1 root root    19288 Jan 18 13:50 ./org.opennms.features.ncs.ncs-northbounder-21.0.3.jar
-rw-r--r-- 1 root root    25031 Jan 18 13:45 ./org.opennms.features.ncs.ncs-persistence-21.0.3.jar
-rw-rw-r-- 1 root root     9602 Jan 18 13:51 ./org.opennms.protocols.cifs-21.0.3.jar
-rw-rw-r-- 1 root root    41591 Jan 18 13:50 ./org.opennms.protocols.dhcp-21.0.3.jar
-rw-rw-r-- 1 root root    63485 Jan 18 13:50 ./org.opennms.protocols.nsclient-21.0.3.jar
-rw-rw-r-- 1 root root    25416 Jan 18 13:50 ./org.opennms.protocols.radius-21.0.3.jar
-rw-rw-r-- 1 root root    47068 Jan 18 13:50 ./org.opennms.protocols.xmp-21.0.3.jar
-rw-rw-r-- 1 root root   637560 Jan 18 13:51 ./vtd-xml-2.11.jar
-rw-rw-r-- 1 root root    88198 Jan 18 13:50 ./xmp-1.40.jar
[root]#

UNIX: How to print column nicely using printf

[user@hostfwnms1-oam tmp]# cat b.sh printf "%-26s %-19s %-8s %-8s %-s %-s\n" HOSTNAME IP PING SNMPWALK 0-ok 1-fail for i in `cat n...