Question
Please help me find the 2 problems with this script. The first involves an error in the output about a missing command. The second is
Please help me find the 2 problems with this script. The first involves an error in the output about a missing command. The second is the fact that the vmstat command should include a timestamp for each line of output. To address the second problem you will need to read the documentation for vmstat.
Script:
#!/bin/bash date; echo "uptime: $(uptime)" echo "Currently connected:" w echo "--------------------" echo "Last logins:" last -a |head -3 echo "--------------------" echo "Disk and memory usage:" df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}' free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' echo "--------------------" start_log=$(head -1 /var/log/syslog |cut -c 1-12) oom=$(grep -ci kill /var/log/syslog) echo -n "OOM errors since $start_log :" $oom echo "" echo "--------------------" echo "Utilization and most expensive processes:" top -b |head -3 echo top -b |head -10 |tail -4 echo "--------------------" echo "Open TCP ports:" nmap -p- -T4 127.0.0.1 echo "--------------------" echo "Current connections:" ss -s echo "--------------------" echo "processes:" ps auxf --width=200 echo "--------------------" echo "vmstat:" vmstat 1 5
Below is example output of the script.
tmoyer2@itis-3246:~/scripting-2$ ./1-server-health.sh Tue Sep 11 12:50:28 EDT 2018 uptime: 12:50:28 up 5 min, 1 user, load average: 0.18, 0.59, 0.33 Currently connected: 12:50:28 up 5 min, 1 user, load average: 0.18, 0.59, 0.33 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT tmoyer2 tty7 :0 12:46 4:51 4.42s 0.01s /bin/sh /etc/xdg/xfce4/xinitrc -- /etc/X11/xinit/xserverrc -------------------- Last logins: tmoyer2 tty7 Tue Sep 11 12:46 gone - no logout :0 reboot system boot Tue Sep 11 12:45 still running 4.15.0-33-generic tmoyer2 tty7 Thu Sep 6 10:07 - 13:20 (03:13) :0 -------------------- Disk and memory usage: Free/total disk: 452M / 452M Free/total memory: 888 / 962 MB -------------------- OOM errors since Sep 6 10:03 : 39 -------------------- Utilization and most expensive processes: top - 12:50:28 up 5 min, 1 user, load average: 0.18, 0.59, 0.33 Tasks: 247 total, 1 running, 147 sleeping, 0 stopped, 0 zombie %Cpu(s): 13.2 us, 7.0 sy, 3.3 ni, 55.6 id, 20.5 wa, 0.0 hi, 0.5 si, 0.0 st PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 159600 6980 5360 S 0.0 0.7 0:01.17 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0 -------------------- Open TCP ports: Starting Nmap 7.60 ( https://nmap.org ) at 2018-09-19 16:26 EDT Nmap scan report for localhost (127.0.0.1) Host is up (0.000031s latency). Not shown: 65534 closed ports PORT STATE SERVICE 631/tcp open ipp Nmap done: 1 IP address (1 host up) scanned in 0.51 seconds -------------------- Current connections: Total: 1366 (kernel 0) TCP: 3 (estab 0, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0 Transport Total IP IPv6 * 0 - - RAW 1 0 1 UDP 7 5 2 TCP 3 2 1 INET 11 7 4 FRAG 0 0 0 -------------------- processes: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2 0.0 0.0 0 0 ? S 12:45 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/0:0] root 4 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kworker/0:0H] root 5 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:0] root 6 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [mm_percpu_wq] root 7 0.1 0.0 0 0 ? S 12:45 0:00 \_ [ksoftirqd/0] root 8 0.0 0.0 0 0 ? I 12:45 0:00 \_ [rcu_sched] root 9 0.0 0.0 0 0 ? I 12:45 0:00 \_ [rcu_bh] root 10 0.0 0.0 0 0 ? S 12:45 0:00 \_ [migration/0] root 11 0.0 0.0 0 0 ? S 12:45 0:00 \_ [watchdog/0] root 12 0.0 0.0 0 0 ? S 12:45 0:00 \_ [cpuhp/0] root 13 0.0 0.0 0 0 ? S 12:45 0:00 \_ [kdevtmpfs] root 14 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [netns] root 15 0.0 0.0 0 0 ? S 12:45 0:00 \_ [rcu_tasks_kthre] root 16 0.0 0.0 0 0 ? S 12:45 0:00 \_ [kauditd] root 17 0.0 0.0 0 0 ? S 12:45 0:00 \_ [khungtaskd] root 18 0.0 0.0 0 0 ? S 12:45 0:00 \_ [oom_reaper] root 19 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [writeback] root 20 0.0 0.0 0 0 ? S 12:45 0:00 \_ [kcompactd0] root 21 0.0 0.0 0 0 ? SN 12:45 0:00 \_ [ksmd] root 22 0.0 0.0 0 0 ? SN 12:45 0:00 \_ [khugepaged] root 23 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [crypto] root 24 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kintegrityd] root 25 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kblockd] root 26 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [ata_sff] root 27 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [md] root 28 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [edac-poller] root 29 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [devfreq_wq] root 30 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [watchdogd] root 31 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:1] root 32 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/0:1] root 34 0.4 0.0 0 0 ? S 12:45 0:01 \_ [kswapd0] root 35 0.0 0.0 0 0 ? S 12:45 0:00 \_ [ecryptfs-kthrea] root 77 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kthrotld] root 78 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [acpi_thermal_pm] root 79 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_0] root 80 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_0] root 81 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_1] root 82 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_1] root 83 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:2] root 85 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:3] root 88 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [ipv6_addrconf] root 89 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/0:2] root 98 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kstrp] root 115 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [charger_manager] root 116 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:4] root 155 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/0:3] root 170 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [mpt_poll_0] root 171 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [mpt/0] root 172 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_2] root 173 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_2] root 174 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_3] root 175 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_3] root 176 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_4] root 177 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_4] root 178 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_5] root 179 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_5] root 180 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_6] root 181 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_6] root 182 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_7] root 183 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_7] root 184 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_8] root 185 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_8] root 186 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_9] root 187 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_9] root 188 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_10] root 189 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_10] root 190 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_11] root 191 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_11] root 192 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_12] root 193 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_12] root 194 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_13] root 195 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_13] root 196 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_14] root 197 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_14] root 198 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_15] root 199 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_15] root 200 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_16] root 201 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_16] root 202 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_17] root 203 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_17] root 204 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_18] root 205 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_18] root 206 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_19] root 207 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_19] root 208 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_20] root 209 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_20] root 210 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_21] root 211 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_21] root 212 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_22] root 213 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_22] root 214 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_23] root 215 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_23] root 216 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_24] root 217 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_24] root 218 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_25] root 219 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_25] root 220 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_26] root 221 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_26] root 222 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_27] root 223 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_27] root 224 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_28] root 225 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_28] root 226 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_29] root 227 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_29] root 228 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_30] root 229 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_30] root 230 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_31] root 231 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_31] root 232 0.0 0.0 0 0 ? S 12:45 0:00 \_ [scsi_eh_32] root 233 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [scsi_tmf_32] root 234 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:5] root 235 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:6] root 236 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:7] root 237 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:8] root 238 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:9] root 239 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:10] root 240 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:11] root 241 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:12] root 242 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:13] root 243 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:14] root 244 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:15] root 245 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:16] root 246 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:17] root 247 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:18] root 248 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:19] root 249 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:20] root 250 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:21] root 251 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:22] root 252 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:23] root 253 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:24] root 254 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:25] root 255 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:26] root 256 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:27] root 257 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:28] root 258 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:29] root 259 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:30] root 260 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:31] root 261 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [ttm_swap] root 262 0.0 0.0 0 0 ? I 12:45 0:00 \_ [kworker/u256:32] root 263 0.0 0.0 0 0 ? S 12:45 0:00 \_ [irq/16-vmwgfx] root 265 0.1 0.0 0 0 ? I< 12:45 0:00 \_ [kworker/0:1H] root 287 0.0 0.0 0 0 ? S 12:45 0:00 \_ [jbd2/sda1-8] root 288 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [ext4-rsv-conver] root 517 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kworker/u257:0] root 519 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kworker/u257:1] root 520 0.0 0.0 0 0 ? I< 12:45 0:00 \_ [kworker/u257:2] root 1971 0.0 0.0 0 0 ? S< 12:46 0:00 \_ [krfcommd] root 10941 0.0 0.0 0 0 ? I< 12:47 0:00 \_ [xfsalloc] root 10947 0.0 0.0 0 0 ? I< 12:47 0:00 \_ [xfs_mru_cache] root 10950 0.0 0.0 0 0 ? S 12:47 0:00 \_ [jfsIO] root 10953 0.0 0.0 0 0 ? S 12:47 0:00 \_ [jfsCommit] root 10954 0.0 0.0 0 0 ? S 12:47 0:00 \_ [jfsSync] root 1 0.3 0.7 159600 6980 ? Ss 12:45 0:01 /sbin/init splash root 324 0.0 1.3 94944 13572 ? S
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started