uptime check (470 Views)
Reply
Frequent Advisor
tempsample
Posts: 100
Registered: ‎06-20-2013
Message 1 of 6 (470 Views)

uptime check

Hi ,

 

 

>>uptime
  7:40pm  up 121 days, 14:47,  1 user,  load average: 0.37, 0.30, 0.30

>>uptime | awk -F, '{sub(".*up ",x,$1);print $1,$2}'
121 days  14:47

Can you please tell me,How the awk command is working for above uptime.

 

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,085
Registered: ‎03-06-2006
Message 2 of 6 (462 Views)

Re: uptime check, awk sub function

[ Edited ]

>How the awk command is working?

>awk -F, '{sub(".*up ", x, $1); print $1, $2}'

 

This says split fields based on comma: $1="7:40pm  up 121 days" $2=" 14:47" $3=" 1 user" ...

Then is uses the sub function on $1 to replace an ERE with the first part of the string, up to "up ".

It replaces it by the variable "x" which is initialized to an empty string.

A better command would be to use a string constant: sub(".*up ", "", $1)

Frequent Advisor
tempsample
Posts: 100
Registered: ‎06-20-2013
Message 3 of 6 (453 Views)

Re: uptime check, awk sub function

Hi Dennis,

 

Thanks for suggestion.

 

   for HOST in $(< /tmp/hostdetails.txt); do
   echo
   ssh -q $HOST uptime | awk -F, '{sub(".*up", "", $1);print $1,$2}'
   done

 

 

I am writing a script which checks the uptime of server.

 

I am running the script from a centralized server.

 

I need all uptime logs from other  server to be availble in centralized server.

 

How can I do it ?

 

above script displays only uptime of all server,but I have to collect all uptime logs in centralized server.

Acclaimed Contributor
Dennis Handly
Posts: 25,085
Registered: ‎03-06-2006
Message 4 of 6 (445 Views)

Re: uptime check, awk sub function

>I need all uptime logs from other server to be available in centralized server.

 

If you run the script on your central server, you can simply redirect stdout:

   echo $HOST

...

done >> uptime.log

 

This will append any stdout output in the loop to uptime.log.

 

 

Frequent Advisor
tempsample
Posts: 100
Registered: ‎06-20-2013
Message 5 of 6 (428 Views)

Re: uptime check, awk sub function

Hi ,

I have two points to be clarified.

 

First Point

 

 

I am running the script from solaris server to check uptime status of hp-ux server.

 

>>for HOST in $(< /tmp/hostdetails.txt); do

 

when I use above format,I am getting error : syntax error $ unexpected.

 

 

but it is running fine with `cat /tmp/hostdetails.txt`

 

I need to know why i am getting syntax error for above cmd.

Second Point :

 

for example if a server is down or decommissioned,so when i run uptime script ,it gets waiting to get server status,but actually server is down.

 

I need to know how can i avoid this problem.

 

 

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,085
Registered: ‎03-06-2006
Message 6 of 6 (421 Views)

Re: uptime check, awk sub function

>I am running the script from solaris server to check uptime status of HP-UX server.

 

Then your question doesn't belong in an HP-UX forum.

 

>when I use above format, I am getting error: syntax error $ unexpected.

 

I assume that Solaris has some real shells, ksh/Posix?  And not scummy csh/tcsh nor Bourne sh.

 

>for example if a server is down or decommissioned

 

See your other topic for setting ssh timeouts:

http://h30499.www3.hp.com/t5/Languages-and-Scripting/setting-timeout-value/m-p/6346969#M46743

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.