4

we have redhat servers - 7.2

the following output from sar print all relevant details as the following

sar -p -d 1 1


07:16:35 PM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
07:16:36 PM       sda     13.00      0.00    120.00      9.23      0.04      3.08      1.38      1.80
07:16:36 PM vg_livecd-lv_root     15.00      0.00    120.00      8.00      0.05      3.07      1.27      1.90
07:16:36 PM vg_livecd-lv_swap      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
07:16:36 PM vg_livecd-lv_home      0.00      0.00      0.00      0.00      0.00      0.00 

we want now to add the hostname of the machine in the beginning of each line

first we found the hostname

hostname=` hostname `

echo $hostname

server_mng14

expected results

sar -p -d 1 1


server_mng14 07:16:35 PM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
server_mng14 07:16:36 PM       sda     13.00      0.00    120.00      9.23      0.04      3.08      1.38      1.80
server_mng14 07:16:36 PM vg_livecd-lv_root     15.00      0.00    120.00      8.00      0.05      3.07      1.27      1.90
server_mng14 07:16:36 PM vg_livecd-lv_swap      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
server_mng14 07:16:36 PM vg_livecd-lv_home      0.00      0.00      0.00      0.00      0.00      0.00 

what we need to pipe after - sar -p -d 1 1 in order to get the hostname of the beginning of each line?

  • 1
    sar on my system shows Linux 4.4.38 (comp) 09/24/2019 _x86_64_ (8 CPU) header on the top but it's missing in your example. What version of sar do you use? – Arkadiusz Drabczyk Sep 24 at 15:00
  • sysstat version 10.1.5 (C) Sebastien Godard (sysstat <at> orange.fr) – yael Sep 24 at 15:01
  • Ok, I use 11.2.1.1. Are you sure there is no header in output of sar or you just removed it? – Arkadiusz Drabczyk Sep 24 at 15:02
  • I not understand why this is important because what I want is to add the hostname in the first field of the output , it could be any other command – yael Sep 24 at 15:04
  • This is important because you post incorrect input that only causes confusion and expect people to help you. And the header already contains hostname so comp Linux 4.4.38 (comp) 09/24/2019 _x86_64_ (8 CPU) looks weird. – Arkadiusz Drabczyk Sep 24 at 15:06
10

You could run:

sar -p -d 1 1 | sed "s/^/$(hostname) /"
  • what in case I run the sar cli on remote machine , so how in this case I set the hostname of remote machine inside sed? – yael Sep 24 at 15:12
  • 3
    ssh user@remote-machine 'sar -p -d 1 1 | sed "s/^/$(hostname) /"' – rusty shackleford Sep 24 at 15:14
8

You can do:

sar -p -d 1 1 | sed "s,^,$(hostname) ,"

if you want to prepend hostname to only non-empty lines:

sar -p -d 1 1 | sed -E "s,^(.+),$(hostname) \1,"
  • what in case I run the sar cli on remote machine , so how in this case I set the hostname of remote machine inside sed? – yael Sep 24 at 15:12
  • How do you run it? Do you use SSH? – Arkadiusz Drabczyk Sep 24 at 15:12
  • example - res=` ssh $remote_machine "sar -p -d 1 1" , then echo "$res" | .... – yael Sep 24 at 15:14
  • @yael: your example is not correct. Fix formatting. – Arkadiusz Drabczyk Sep 24 at 15:15
2

An awk version:

sar -p -d 1 1 | awk -v HOSTNAME=$HOSTNAME '{print HOSTNAME " " $0}'

with the -v flag we set the bash $HOSTNAME variable to a variable that awk can reference in its print statement. With $0 we print the entire line

Or on ssh:

ssh user@remote "sar -p -d 1 1 | awk -v HOSTNAME=$HOSTNAME '{print HOSTNAME \" \" \$0}'"
  • You're missing a whitespace: '{print HOSTNAME " " $0}' – Arkadiusz Drabczyk Sep 25 at 14:41
  • You're right, thanks for pointing that out. I also fixed some escaping issues for the ssh command – F. Pareto Sep 25 at 14:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.