Linux-Fu Part 2

Figured it’s high time for a nerd SysAdmin blog post.  This one will be particularly bland with some commands and tools I use to administer Linux systems.

Ever wanted to get a quick and dirty count of open files by process?

lsof | awk '{ print $1" "$2; }' | sort | uniq -c | sort -n

This is pretty useful when you’re starting to deep dive a system.

Take netstat to the next level and  look for active server network ports so you can tell users you support that their app is running

[root@deadbeef ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 127.0.0.1:4803              0.0.0.0:*                   LISTEN      4083/spread
tcp        0      0 172.25.47.172:4803          0.0.0.0:*                   LISTEN      4083/spread
tcp        0      0 0.0.0.0:5444                0.0.0.0:*                   LISTEN      5138/python   
tcp        0      0 0.0.0.0:199                 0.0.0.0:*                   LISTEN      3673/snmpd 
tcp        0      0 0.0.0.0:587                 0.0.0.0:*                   LISTEN      4019/sendmail
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      8942/sshd 
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      4019/sendmail

Ever wanted to get dmidecode in a somewhat useful format?

[root@deadbeef ~]# for kw in $(dmidecode -s 2>&1 | sed -n '3,$p'); do echo -en "e[1m${kw}:e[0m "; echo $(dmidecode -s ${kw}); done
bios-vendor: Dell Inc.
bios-version: 2.3.1
bios-release-date: 04/29/2008
system-manufacturer: Dell Inc.
system-product-name: PowerEdge 1950
system-version: Not Specified
system-serial-number:
baseboard-manufacturer: Dell Inc.
baseboard-product-name: 0H878G
baseboard-version: A01
baseboard-serial-number: ..CN6970284D1596.
baseboard-asset-tag: Not Specified
chassis-manufacturer: Dell Inc.
chassis-version: Not Specified
chassis-serial-number: 
chassis-asset-tag: Not Specified
processor-manufacturer: Intel Intel
processor-version: Intel(R) Xeon(R) CPU X5460 @ 3.16GHz Not Specified

Determining System Age

So today, I was approached with a rare question of “how old are these Linux and Windows systems?”  Since we do not have a robust inventory or cms system at work that documents host provisioning/installation dates, I am forced to trust what the system has locally to determine system age.

Digging around the net with Jimmy Google and consulting peers, the best method of determining this in Linux is to  look at the basesystem package provisioned upon system installation.

[jdoe@deadbeef ~]$ sudo rpm -qi basesystem
Password:
Name        : basesystem                   Relocations: (not relocatable)
Version     : 8.0                               Vendor: CentOS
Release     : 4                             Build Date: Mon 21 Feb 2005 01:37:18 PM PST
Install Date: Mon 16 Oct 2006 04:57:55 AM PDT      Build Host: build2.hughesjr.centos.org
Group       : System Environment/Base       Source RPM: basesystem-8.0-4.src.rpm
Size        : 0                                License: public domain
Signature   : DSA/SHA1, Sat 26 Feb 2005 12:31:22 PM PST, Key ID a53d0bab443e1821
Packager    : Johnny Hughes
Summary     : The skeleton package which defines a simple Red Hat Linux system.
Description :
Basesystem defines the components of a basic Red Hat Linux system (for example, the package installation order to use during bootstrapping). Basesystem should be the first package installed on a system, and it should never be removed.

In Windows, this is possible in a cmd prompt as follows:

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\jdoe>systeminfo | find /i "install date"
Original Install Date:     9/27/2012, 4:46:47 PM