Showing posts with label POSIX. Show all posts
Showing posts with label POSIX. Show all posts

Friday, February 17, 2023

NAWK: How can I get uptime & boot time in Solaris?

 

[Solaris Logo, courtesy former Sun Microsystems, Oracle]

Abstract:

This is part of a small article series on lesser known uses of UNIX nawk. Why nawk? This POSIX language has not changed in decades, gets very few code changes, and is mostly bug free in any reasonable operating system. Scripts written in POSIX nawk will basically run, forever. Master this simple language, and there is virtually nothing that can not be done.

What is AWK?

Awk is a scriptable text language, originally written by the authors of UNIX and C: Alfred Aho, Peter Weinberger, and Brian Kernighan. It's concepts are managing Page Header (the BEGIN clause), Page Body (multiple Pattern -> Action clauses), and Page Footer (the END clause.) 

Awk is suitable for building & manipulating any textual or markup text, which is page oriented (i.e. SQL output, Text, Adobe PostScript, Adobe FrameMaker portable maker format, HTML, XML, Microsoft RTF, Microsoft CSV, etc.)

NAWK: How can I get uptime & boot time in Solaris?

The CTime or Epoch Time can be easily retrieved from nawk using the "srand()" function, if there is no seed passed into the function. This number represents the seconds since 00:00:00 on January 1, 1970.

The boot time is the CTime/Epoch Time [in seconds], at the time of the last OS boot, and can be retrieved from "kstat". Simple subtraction and division can provide reasonably accurate conversions.

The following examples show: CTime, Boot Time, Seconds Difference, Minutes Difference, Hours Difference, Days Difference.

Example 1 - Solaris 10:

Solaris10/root# kstat -n system_misc | nawk '
BEGIN { CTime=srand() };
/boot_time/ { BTime=$2; Diff=CTime-BTime };
END { printf "CTime:\t%s\nBTime:\t%s\nScDif:\t%s\nMnDif:\t%s\nHrDif:\t%s\nDyDif:\t%s\n",
      CTime,BTime,Diff,Diff/60,Diff/60/60,Diff/60/60/24 }'

CTime:  1676683570
BTime:  1676106161
ScDif:  577409
MnDif:  9623.48
HrDif:  160.391
DyDif:  6.68297

Example 2 - Solaris 11:

Solaris 11/root# kstat -n system_misc | nawk '
BEGIN { CTime=srand() };
/boot_time/ { BTime=$2; Diff=CTime-BTime };
END { printf "CTime:\t%s\nBTime:\t%s\nScDif:\t%s\nMnDif:\t%s\nHrDif:\t%s\nDyDif:\t%s\n",
      CTime,BTime,Diff,Diff/60,Diff/60/60,Diff/60/60/24 }'

CTime:  1676683533
BTime:  1662691183
SecDif: 13992350
MinDif: 233206
HrDif:  3886.76
DayDif: 161.948

Does the code look similar? It should, Solaris 10 and Solaris 11 are POSIX compliant.

What about GAWK?

GNU's Awk is not 100% compatible with POSIX Nawk, which is a huge disappointment. I have seen code, which worked for decades, never able to run unchanged under GNU's awk.

 

Tuesday, May 29, 2012

Network Management Hint: Sort by IP Address

Network Management Hint: Sort by IP Address

How do you sort a host table by IP Address on a UNIX network management platform?

This has been a basic requirement, since the dawn of The Internet.

Let's look at an example /etc/hosts extract:
sun9999/user$ cat /etc/hosts
#
127.0.0.1       localhost
::1             localhost

201.14.24.17    BT-Site-1       # LasPalmas
203.16.54.112   QB-Site-1       # NorthSydney
201.14.24.21    DL-Site-1       # LasPalmas
202.135.192.97  QB-Site-1       # NorthSydney
203.16.54.18    PR-Site-1       # NorthSydney
201.14.24.28    DL-Site-2       # LasPalmas
203.16.54.22    KP-Site-1       # NorthSydney

Each octet in an IPv4 address is separated by a period "." and a simple POSIX sort will use the period as the field separator and then request a numeric sort on the first, second, third, and fourth octets.
sun9999/user$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts

::1 localhost
#
127.0.0.1
       localhost
201.14.24.17
    BT-Site-1     # LasPalmas
201.14.24.21
    DL-Site-1     # LasPalmas
201.14.24.28
    DL-Site-2     # LasPalmas
202.135.192.97
  QB-Site-1     # NorthSydney
203.16.54.18
    PR-Site-1     # NorthSydney
203.16.54.22
    KP-Site-1     # NorthSydney
203.16.54.112
   QB-Site-1     # NorthSydney

What it you want to eliminate colon ":" separated IPv6 addresses, empty lines, and lines with comments? Just perform a little POSIX awk'ing after the sort.
sun9999/user$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts | awk '$1!~/:/ && !/^$/ && !/^#/'
127.0.0.1       localhost
201.14.24.17    BT-Site-1       # LasPalmas
201.14.24.21    DL-Site-1       # LasPalmas
201.14.24.28    DL-Site-2       # LasPalmas
202.135.192.97  QB-Site-1       # NorthSydney
203.16.54.18    PR-Site-1       # NorthSydney
203.16.54.22    KP-Site-1       # NorthSydney
203.16.54.112   QB-Site-1       # NorthSydney
Want to eliminate all localhost entries? Add a localhost elimination pattern:
sun9999/user$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts | awk '$1!~/:/ && !/^$/ && !/^#/ && !/localhost/'
201.14.24.17    BT-Site-1       # LasPalmas
201.14.24.21    DL-Site-1       # LasPalmas
201.14.24.28    DL-Site-2       # LasPalmas
202.135.192.97  QB-Site-1       # NorthSydney
203.16.54.18    PR-Site-1       # NorthSydney
203.16.54.22    KP-Site-1       # NorthSydney
203.16.54.112   QB-Site-1       # NorthSydney
Need to print only the host name, sorted by IP Address, for entries managed by NorthSydney NOC?
sun9999/user$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts | 
awk '$1!~/:/ && !/^$/ && !/^#/ && !/localhost/ && /NorthSydney/ { 
print $2 }'
QB-Site-1
PR-Site-1
KP-Site-1
QB-Site-1


If you are not using POSIX commands to do standard network management work, you don't know what you are missing!