├── ping-port-test-1001.sh └── memstat.sh /ping-port-test-1001.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # This shell script enables you to perform a ping to remote host 5 | # and check whether the mentioned port is opened on that server. 6 | # This helps system admin for ping test and also make sure 7 | # if there any issues with specific ports. 8 | 9 | Parent : http://www.linoxide.com/guide/scripts-pdf.html 10 | Source : http://www.linoxide.com/how-tos/ping-check-open-port/ 11 | 12 | 13 | # check if service name passed to script as argument, if there no arguments (0) do next 14 | 15 | if [ "$#" = "0" ]; 16 | 17 | then 18 | 19 | #write to terminal usage 20 | 21 | echo �Usage: $0 � 22 | 23 | #since no arguments � we need to exit script and user re-run 24 | 25 | exit 1 26 | fi 27 | 28 | #writing parameters to variables 29 | 30 | host=$1 31 | port=$2 32 | email=�test@expertslogin.com� 33 | subject=�Script result� 34 | 35 | #Check if ping ok -q to quite mod, -c 4 for 4 checks 36 | 37 | if ping -q -c 4 $host >/dev/null 38 | then 39 | # next lines writes result variable 40 | 41 | ping_result=�OK� 42 | else 43 | ping_result=�NOT OK� 44 | 45 | fi #end of fi loop 46 | 47 | #next command check if port opened via nc command, and getting exit status of nc command 48 | 49 | nc_result=`nc -z $host $port; echo $?` 50 | 51 | #check of exit status of nc command, and write results to variables 52 | 53 | if [ $nc_result != 0 ]; 54 | then 55 | port_result=�not opened� 56 | else 57 | port_result=�opened� 58 | fi #exit of fi loop 59 | 60 | #writing message that script will email and write to output 61 | 62 | message=�Ping to host � ${ping_result}, port $port ${port_result}.� 63 | 64 | #next check if ping or port check is failed (ping if not OK and exit status of nc if not 0) 65 | 66 | if [ "$ping_result" != "OK" -o "$nc_result" != "0" ]; 67 | then 68 | echo �$message� #this line write warning message to terminal 69 | 70 | echo �$message� | mail -s �$subject� $email #this line send email 71 | 72 | fi 73 | 74 | -------------------------------------------------------------------------------- /memstat.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # Memstat.sh is a shell script that calculates linux memory usage for each program / application. 5 | # Script outputs shared and private memory for each program running in linux. Since memory calculation is bit complex, 6 | # this shell script tries best to find more accurate results. Script use 2 files ie /proc//status (to get name of process) 7 | # and /proc//smaps for memory statistic of process. Then script will convert all data into Kb, Mb, Gb. 8 | # Also make sure you install bc command. 9 | 10 | 11 | Source : http://www.linoxide.com/linux-shell-script/linux-memory-usage-program/ 12 | Parent : http://www.linoxide.com/guide/scripts-pdf.html 13 | 14 | # Make sure only root can run our script 15 | 16 | if [ "$(id -u)" != "0" ]; then 17 | echo "This script must be run as root" 1>&2 18 | exit 1 19 | fi 20 | 21 | ### Functions 22 | #This function will count memory statistic for passed PID 23 | get_process_mem () 24 | { 25 | PID=$1 26 | #we need to check if 2 files exist 27 | if [ -f /proc/$PID/status ]; 28 | then 29 | if [ -f /proc/$PID/smaps ]; 30 | then 31 | #here we count memory usage, Pss, Private and Shared = Pss-Private 32 | Pss=`cat /proc/$PID/smaps | grep -e "^Pss:" | awk '{print $2}'| paste -sd+ | bc ` 33 | Private=`cat /proc/$PID/smaps | grep -e "^Private" | awk '{print $2}'| paste -sd+ | bc ` 34 | #we need to be sure that we count Pss and Private memory, to avoid errors 35 | if [ x"$Rss" != "x" -o x"$Private" != "x" ]; 36 | then 37 | 38 | let Shared=${Pss}-${Private} 39 | Name=`cat /proc/$PID/status | grep -e "^Name:" |cut -d':' -f2` 40 | #we keep all results in bytes 41 | let Shared=${Shared}*1024 42 | let Private=${Private}*1024 43 | let Sum=${Shared}+${Private} 44 | 45 | echo -e "$Private + $Shared = $Sum \t $Name" 46 | fi 47 | fi 48 | fi 49 | } 50 | 51 | #this function make conversion from bytes to Kb or Mb or Gb 52 | convert() 53 | { 54 | value=$1 55 | power=0 56 | #if value 0, we make it like 0.00 57 | if [ "$value" = "0" ]; 58 | then 59 | value="0.00" 60 | fi 61 | 62 | #We make conversion till value bigger than 1024, and if yes we divide by 1024 63 | while [ $(echo "${value} > 1024"|bc) -eq 1 ] 64 | do 65 | value=$(echo "scale=2;${value}/1024" |bc) 66 | let power=$power+1 67 | done 68 | 69 | #this part get b,kb,mb or gb according to number of divisions 70 | case $power in 71 | 0) reg=b;; 72 | 1) reg=kb;; 73 | 2) reg=mb;; 74 | 3) reg=gb;; 75 | esac 76 | 77 | echo -n "${value} ${reg} " 78 | } 79 | 80 | #to ensure that temp files not exist 81 | [[ -f /tmp/res ]] && rm -f /tmp/res 82 | [[ -f /tmp/res2 ]] && rm -f /tmp/res2 83 | [[ -f /tmp/res3 ]] && rm -f /tmp/res3 84 | 85 | 86 | #if argument passed script will show statistic only for that pid, of not � we list all processes in /proc/ #and get statistic for all of them, all result we store in file /tmp/res 87 | if [ $# -eq 0 ] 88 | then 89 | pids=`ls /proc | grep -e [0-9] | grep -v [A-Za-z] ` 90 | for i in $pids 91 | do 92 | get_process_mem $i >> /tmp/res 93 | done 94 | else 95 | get_process_mem $1>> /tmp/res 96 | fi 97 | 98 | 99 | #This will sort result by memory usage 100 | cat /tmp/res | sort -gr -k 5 > /tmp/res2 101 | 102 | #this part will get uniq names from process list, and we will add all lines with same process list 103 | #we will count nomber of processes with same name, so if more that 1 process where will be 104 | # process(2) in output 105 | for Name in `cat /tmp/res2 | awk '{print $6}' | sort | uniq` 106 | do 107 | count=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $6}}'|wc -l| awk '{print $1}'` 108 | if [ $count = "1" ]; 109 | then 110 | count="" 111 | else 112 | count="(${count})" 113 | fi 114 | 115 | VmSizeKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $1}}' | paste -sd+ | bc` 116 | VmRssKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $3}}' | paste -sd+ | bc` 117 | total=`cat /tmp/res2 | awk '{print $5}' | paste -sd+ | bc` 118 | Sum=`echo "${VmRssKB}+${VmSizeKB}"|bc` 119 | #all result stored in /tmp/res3 file 120 | echo -e "$VmSizeKB + $VmRssKB = $Sum \t ${Name}${count}" >>/tmp/res3 121 | done 122 | 123 | 124 | #this make sort once more. 125 | cat /tmp/res3 | sort -gr -k 5 | uniq > /tmp/res 126 | 127 | #now we print result , first header 128 | echo -e "Private \t + \t Shared \t = \t RAM used \t Program" 129 | #after we read line by line of temp file 130 | while read line 131 | do 132 | echo $line | while read a b c d e f 133 | do 134 | #we print all processes if Ram used if not 0 135 | if [ $e != "0" ]; then 136 | #here we use function that make conversion 137 | echo -en "`convert $a` \t $b \t `convert $c` \t $d \t `convert $e` \t $f" 138 | echo "" 139 | fi 140 | done 141 | done < /tmp/res 142 | 143 | #this part print footer, with counted Ram usage 144 | echo "--------------------------------------------------------" 145 | echo -e "\t\t\t\t\t\t `convert $total`" 146 | echo "========================================================" 147 | 148 | # we clean temporary file 149 | [[ -f /tmp/res ]] && rm -f /tmp/res 150 | [[ -f /tmp/res2 ]] && rm -f /tmp/res2 151 | [[ -f /tmp/res3 ]] && rm -f /tmp/res3 152 | --------------------------------------------------------------------------------