The officially official Devuan Forum!

You are not logged in.

#1 2022-05-12 14:06:30

Altoid
Member
Registered: 2017-05-07
Posts: 1,049  

[SOLVED] Script printout format assistance

Hello:

I have this script running on ssh login:

#!/bin/sh
PATH="/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin"
echo uptime:
# run uptime - no load stats
uptime | cut -c 2-19
echo
echo rsync daemon status:
# run alias daemon
sudo /etc/init.d/rsyncd status
echo
echo memory status:
cat /proc/meminfo | grep -i memavailable && cat /proc/meminfo | grep -i memfree
echo
echo filesystem status:
# run alias fschk
df -h | grep -vE '^Filesystem|/dev/root|tmpfs'| awk '{ print $5 " " $1}'
echo
echo drive temperature:
# run alias tempchk
sudo smartctl -d ata -A /dev/sda | grep Temperature | cut -c 5-8,87-89
echo
echo filesystem health:
# run alias drivechk
sudo dmesg | grep -i ext4-fs | grep -i sda

This is the terminal printout:

uptime:
10:46:23 up 0 min,

rsync daemon status:
running

memory status:
MemAvailable:      211716 kB
MemFree:           228924 kB

filesystem status:
53% /dev/sda1
58% /dev/sda3

drive temperature:
Temp 22

How can I get it to print out in this format?

uptime:              10:46:23 up 0 min,

rsync daemon status: running

memory status:
MemAvailable:        211716 kB
MemFree:             228924 kB

filesystem status:   53% /dev/sda1
                     58% /dev/sda3

drive temperature:   Temp 22

Thanks in advance.

Best,

A.

Edit: spelling

Last edited by Altoid (2022-05-14 21:18:12)

Offline

#2 2022-05-12 14:28:17

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 2,381  

Re: [SOLVED] Script printout format assistance

#!/bin/sh

_uptime=$(uptime|cut -c 2-19)
_rsync_status=$(/etc/init.d/rsyncd status) # does not need root!
_memavail=$(grep -i memavailable /proc/meminfo) # save those poor cats!
_memfree=$(grep -i memfree /proc/meminfo) # ditto
_fs_status=$(df -h|awk '!/^Filesystem/&&!/\/dev\/root/&&!/tmpfs/{ print $5 " " $1}') # awk can search so no need for grep
_drive_temp=$(sudo smartctl -d ata -A /dev/sda | grep Temperature | cut -c 5-8,87-89) 

printf '%s\t\t%s\n\n' "uptime:" "$_uptime" # \t adds tab characters, \n adds new lines, %s defines strings which are listed afterwards

printf '%s\t%s\n\n' "rsync daemon status:" "$_rsync_status"

printf '%s\n%s\n%s\n\n' "memory status:" "$_memavail" "$_memfree"

printf '%s\t%s\n\n' "filesystem status:" "$_fs_status" # might not work :/

printf '%s\t%s\n' "drive temperature" "$_drive_temp"

I'm trapped in Windows atm so this is untested, I'll post back later when I'm in a proper OS...

EDIT: fixes...

Last edited by Head_on_a_Stick (2022-05-13 10:15:37)


To obtain a root shell use su -. Using just su will result in "command not found" messages.

Offline

#3 2022-05-12 20:17:22

Altoid
Member
Registered: 2017-05-07
Posts: 1,049  

Re: [SOLVED] Script printout format assistance

Hello:

Head_on_a_Stick wrote:
#!/bin/sh
_uptime=$(uptime|cut -c 2-19)
_rsync_status=$(/etc/init.d/rsyncd status) # does not need root!
_memavail=$(grep -i memavailable /proc/meminfo) # save those poor cats!
_memfree=$(grep -i memfree /proc/meminfo) # ditto
_fs_status=$(df -h|awk '/^Filesystem/||/\/dev\/root/||/tmpfs/{ print $5 " " $1}') # awk can search so no need for grep
_drive_temp=$(sudo smartctl -d ata -A /dev/sda | grep Temperature | cut -c 5-8,87-89) 

printf '%s\t\t%s\n\n' "uptime:" "$_uptime" # \t adds tab characters, \n adds new lines, %s defines strings which are listed afterwards

printf '%s\t%s\n\n' "rsync daemon status:" "$_rsync_status"

printf '%s\n%s\n%s\n\n' "memory status:" "$_mem_avail" "$_mem_free"

printf '%s\t%s\n\n' "filesystem status:" "$_fs_status" # might not work :/

printf '%s\t%s\n' "drive temperature" "$_drive_temp"

Right ...

I'll check it out tonight and report back as soon as I figure out how it works.

Head_on_a_Stick wrote:

... trapped in Windows atm ...

You'll manage.  ;^ )

Head_on_a_Stick wrote:

... post back later when I'm in a proper OS...

Thank you for your input.

Best,

A.

Last edited by Altoid (2022-05-12 20:18:58)

Offline

#4 2022-05-14 21:17:08

Altoid
Member
Registered: 2017-05-07
Posts: 1,049  

Re: [SOLVED] Script printout format assistance

Hello:

Altoid wrote:

... check it out ...
... report back as soon as I figure out ...

It took me a while, eventually got a decent result.
Trimmed it a bit so that what I get is just the essential information.

This:

#!/bin/sh
PATH="/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin"

# \t adds tab characters
# \n adds new lines
# %s defines strings which are listed afterwards
# awk can search so no need for grep

_uptime=$(uptime| sed 's/.*up \([^,]*\), .*/\1/')
_rsync_status=$(sudo /etc/init.d/rsyncd status)                   # OK needs root x ash
_memavail=$(grep -i memavailable /proc/meminfo | cut -c 19-27)    # OK no cats harmed
_memfree=$(grep -i memfree /proc/meminfo | cut -c 19-27)          # OK no cats harmed
_fs_status1=$(df -h | grep -vE '^Filesystem|/dev/root|tmpfs' | cut -c 6-70 | grep -i sda1 | cut -c 40-50)
_fs_status2=$(df -h | grep -vE '^Filesystem|/dev/root|tmpfs' | cut -c 6-70 | grep -i sda3 | cut -c 40-50)
_drive_temp=$(sudo smartctl -a -d ata /dev/sda | awk '/Temperature/ {print $10}')

printf '%s\t\t%s\n' "uptime:" "$_uptime"           # OK
printf '%s\t\t%s\n' "daemon:" "$_rsync_status"     # OK
printf '%s\t%s\n' "mem available:" "$_memavail"    # OK
printf '%s\t%s\n' "mem free:" "$_memfree"          # OK
printf '%s\t%s\n' "sda1 free/used:" "$_fs_status1" # OK
printf '%s\t%s\n' "sda3 free/used:" "$_fs_status2" # OK
printf '%s\t' "hd temp:" "$_drive_temp"            # OK

Gets me this when I ssh to the NAS:

~$ ssh user@192.168.1.3
--- snip ---
uptime:		 2:34
daemon:		running
mem available:	210064 kB
mem free:	226420 kB
sda1 free/used:	  3.6M  53%
sda3 free/used:	362.6G  58%
hd temp:	48	
~$ 

Seems good enough and the info is there.
Could not figure out how to get the °C after the temperature [number] print but it is just for me.
And I've been a Celsius man all my life. =^ )

Your pointers on lines, characters and strings were a big help.

Best,

A.

Edit: spelling/format.

Last edited by Altoid (2022-05-14 23:53:01)

Offline

#5 2022-05-15 07:44:34

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 2,381  

Re: [SOLVED] Script printout format assistance

Altoid wrote:

Could not figure out how to get the °C after the temperature [number] print

printf '%s\t%s\n' "hd temp:" "${_drive_temp}°C"
Altoid wrote:

I've been a Celsius man all my life

I prefer Kelvin.


To obtain a root shell use su -. Using just su will result in "command not found" messages.

Offline

#6 2022-05-15 08:22:26

ralph.ronnquist
Administrator
From: Clifton Hill, Victoria, AUS
Registered: 2016-11-30
Posts: 695  

Re: [SOLVED] Script printout format assistance

@Altoid, quite possibly the MemAvailable measure has lost its most significant digit (it's rather unusual to have more free than available memory), and maybe you should consider replacing

_memavail=$(grep -i memavailable /proc/meminfo | cut -c 19-27)    # OK no cats harmed
_memfree=$(grep -i memfree /proc/meminfo | cut -c 19-27)          # OK no cats harmed

with something else, like e.g.

_memavail=$(sed '/memavailable/I!d;s/[^0-9]*//;q' /proc/meminfo)    # OK no cats or cuts
_memfree=$(sed '/memfree/I!d;s/[^0-9]*//;q' /proc/meminfo)          # OK no cats or cuts

EDIT: (though maybe I'm wrong about what memfree and memavailable means?)

Offline

#7 2022-05-15 10:12:57

Altoid
Member
Registered: 2017-05-07
Posts: 1,049  

Re: [SOLVED] Script printout format assistance

Hello:

Head_on_a_Stick wrote:
printf '%s\t%s\n' "hd temp:" "${_drive_temp}°C"

Done.

~$ ssh user@192.168.1.3
--- snip ---
uptime:		9 min
ui:		inactive
daemon:		running
mem available:	208392 kB
mem free:	222252 kB
sda1 free/used:	  3.6M  53%
sda3 free/used:	362.6G  58%
hd temp:	26°C
~$ 
Head_on_a_Stick wrote:

I prefer Kelvin.

Cool!  8^D

Thank you for your input.

Best,

A.

Offline

#8 2022-05-15 10:38:46

Altoid
Member
Registered: 2017-05-07
Posts: 1,049  

Re: [SOLVED] Script printout format assistance

Hello:

ralph.ronnquist wrote:

... possibly the MemAvailable measure has lost ...

I had the exact same thought when I first saw the printout.
But no, it is correct.

The thing is that the /proc/meminfo list prints like this ...

root@OpenWrt:~# cat /proc/meminfo
MemTotal:         251868 kB
MemFree:          222268 kB
MemAvailable:     208408 kB
--- snip ---

... which makes sense as they are usually progressively declining values.
ie: as I understand it (?) total will always be higher than free which will be equal to or higher than available due to buffers, cached, etc. This when swap is not part of the equation as it adds to the available memory.

I think the confusion arises because I put them the wrong way around.
ie: instead of how they are originally listed.

Now it looks like it makes sense:

~$ ssh user@192.168.1.3
--- snip ---
uptime:		32 min
ui:		inactive
daemon:		running
mem free:	222312 kB
mem available:	208392 kB
sda1 free/used:	  3.6M  53%
sda3 free/used:	362.6G  58%
hd temp:	36°C
~# 

Thank you for pointing that out.

Best,

A.

Last edited by Altoid (2022-05-15 12:05:06)

Offline

Board footer