Finding Disk Space Hogs

[problem]

Common admin task, to find what is using all the disk space.

[/problem]

[solution]

Simpliest thing to do, in a known directory – for example /var/log – is to run du -ks *.

That will show all files and directories, in the current directory along with their disk space in kilobytes.

Then cd down the top ones and rerun. You can also pump the output through sort (-n on Solaris and +n on Linux).

[/solution]

[example]


cd /var/log
du -ks * | sort -n # Linux
cd httpd
du -ks * | sort -n

With some flavours of UNIX, du shows actual physical disk space being used – where as df shows disk space being reserved. This is only really noticeable when you remove a file, that is still being written too. du shows the current directory is only using x kb and df still says you are at 100%. 🙂 You need to find and kill the process.

Another useful way of finding largest files, is find command – something like this:


find / -xdev -type f -a size +20000 -ls

This says find files over 10MB (20 thousand 512 byte blocks), do not traverse file systems mounted on this one (-xdev). You can also say -mount to stop find traversing these file systems. That way df and find tally – just re-run it for var, etc as required.

Again this can be pumped through to sort:


find / -xdev -type f -a -size +20000 -ls | sort -k7 -n # linux
find / -xdev -type f -a -size +20000 -ls | sort +6n # Solaris - starts field count from zero

[/example]

[reference]

[tags]UNIX, df, du, Solaris, Unix Coding School[/tags]

[/reference]

Display Directory Usage As Percentage

[problem]

How many times have you been given the job, to clear down space in a UNIX file system?

Ever wanted to see the percentage, on a directory by directory basis.

[/problem]

[solution]

Here is a simple bit of code, to display the current directory’s percentage of over all space available

[/solution]

[example]


(du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '

Here is a run through, checking /usr:


[root@bree usr]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
37%

[root@bree usr]# cd java

[root@bree java]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
4%

[root@bree usr]# cd ../X11R6
[root@bree X11R6]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
2%

[root@bree X11R6]# cd ../lib
[root@bree lib]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
15%

[root@bree local]# cd ../share
[root@bree share]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
13%

[/example]

[reference]

[tags]UNIX, Admin, Unix Coding School[/tags]

[/reference]

Duplicate Line Finder

[problem]

Have you ever duplicated a cron entry, or just wanted to find duplicate lines in a file.

[/problem]

[solution]

Here is a simple one liner, which shows all lines which occur more than once in a file or standard output.

[/solution]

[example]


sort cronfile.223 | uniq -c | awk ' $1 != 1 '

So to check that cron directly – just do this:


crontab -l | uniq -c | awk ' $1 != 1 '

[/example]

[reference]

[tags]UNIX, awk, script, uniq, Unix Coding School[/tags]

[/reference]

Legato Networker Restore

[problem]

You want to perform a restore from a legato networker backup server.
Also called NSR backup system

[/problem]

[solution]

To restore from an NSR (networker) backup system, just run recover.

It is best if you cd into the required directory first.

[/solution]

[example]


cd /var/www/httpd
recover

It will list your last back ups with dir. You can type help for more info.

Or just add, then when done – type recover.

dir shows if the tape containing your backup, is online or not.

[/example]

[reference]

[tags]NSR, Netwoker, Net backup, Legato, Unix Coding School[/tags]

[/reference]

mysql API socket connection

[problem]

Sometimes you need to connect to mysql server with a UNIX socket, rather than a Port.

Generally this is when running multiple versions, on a box when you do not want to
or cannot connect to multiple ports.

[/problem]

[solution]

If you want to connect to the mysql server from the command line, you can use -S.

Or within Perl – append mysql_socket to the dsn

[/solution]

[example]


mysql -uUSER -pPASSWORD -S /tmp/mysql.sock


dbi:mysql:database=database_name;mysql_socket=/tmp/mysql.sock

Where your socket is /tmp/mysql.sock

[/example]

[reference]

[tags]MySQL, Socket, UNIX, Perl, Perl Coding School[/tags]

[/reference]

Check NSR Legato Networker Backups

[problem]

You want to view the back ups performed via Legato Networker NSR.

[/problem]

[solution]

Use mminfo – media manager for NSR. It can show which filesystems were backed up,
when they were backed up, to which tapes and the retension period.

[/solution]

[example]


mminfo -ot -s SERVER -c CLIENT -q 'savetime>=last week' -r 'volume,client,savetime(18),ssid,name,written,pool'


mminfo -ot -s SERVER -c CLIENT -q 'savetime>=last week' -r 'volume,client,savetime(18),ssid,name,written,pool,ssretent'

[/example]

[reference]

For more information on available options, do a man mminfo

[tags]NSR, Backups, Legato, mminfo, Unix Coding School[/tags]

[/reference]

RISC System 6000 Boot LEDs

[problem]

Your RS6000 ain’t booting. 🙂 Or you are just interested what the LEDs mean.

[/problem]

[solution]

See Example

[/solution]

[example]

  1. Power On [LED 100 – 199]
  2. BIST [LED 100 – 199]
  3. POST [LED 201 – 298]
  4. Load Kernel [LED 299]
  5. Configuration [LED 500 – 999]
  6. INIT [LED 553]

init than loads /etc/inittab services – then /sbin/rc.boot (phase 3 of system boot),
/etc/rc (startup shell script), subsystems (cron, qdaemon, tcpip), getty (enable terminals)

[/example]

[reference]

[tags]RISC System bootup LEDs, AIX startup sequence, Unix Coding School[/tags]

[/reference]

Clearing an AIX Print Queue

[problem]

You have jobs stuck in the print queue. You want to clear the queues and remove all jobs.

[/problem]

[solution]

You have to stop all processes and remove the files from spooling directories.

Probably worth taking a backup of these files first. 🙂 All at own risk.

[/solution]

[example]

1.


stopsrc -cs qdaemon # bring down qdaemon and clear qdforks.
stopsrc -s lpd

2.


rm /etc/qconfig.bin # compiled /etc/qconfig

3.


rm /var/spool/lpd/qdir/* # queue requests; JDF

4.


rm /var/spool/lpd/stat/* # info on status of devices

5.


rm /var/spool/lpd/pio/msg* # error message files.

6.


rm /var/spool/qdaemon # temp copies of queue jobs

7.


rm /var/spool/lpd/* # temp copies of remote jobs

8.


ps -ef | grep pio # kill any of these processes; look for piobe, pioformat, pioout

9.


ps -ef | grep qd # kill any of these processes; look for qdforks.

10.


startsrc -s qdaemon # restarting qdaemon
startsrc -s lpd # restarting lpd

11.


lssrc -s qdaemon #look for active
ps -ef | grep qdaemon
lssrc -s lpd # look for active
ps -ef | grep lpd

12.


ps -ef | grep srcmstr # if there are 2 running, kill -9 one of them with higher PID.

13.

As a last resort – reboot

[/example]

[reference]

[tags], Unix Coding School[/tags]

[/reference]

DNS, Sendmail and Building Linux

[problem]

Have been aware of DNS and sendmail for over 10 years, but never really dabbled.

[/problem]

[solution]

Hit a lot of issues, mainly with dodgy clients – like outlook (ended up moving over completely to mozilla’s thunderbird email client!). Also with port forwarding and firewall, default routes, etc.

To set it all up these were my steps:

1) Build RH Linux server with fedora core 4 (5 is out now too I think).
2) Ensure BIND was installed. yum is your friend! 🙂 Do yum –help as root.
3) Edit /etc/named.conf – add my zone file. Always make a bu: cp -i /etc/named.conf /etc/named.conf.$(date +%j)
4) Copied localdomain.zone to puterpet.zone # this domain is retired now. 🙂
5) Modified puterpet.zone to include my info: SOA (start of authority) – in my case I said zion.puterpet.com as this then relates to the ip of broadband router. Bit backward in logic, specify your DNS server with a name registered in your DNS server! 🙂 Then I created an A record and NS for zion; CNAME for www to zion and CNAME for mail; lastly an MX record for mail. I’ll happily supply my zone file if someone wants a peek. 🙂
6) Register domain name with www.domaincentral.com.au – $12.50 a year! 🙂 Give it the ip of my DNS server – which is actually the broadband router, with port 53 forwarded to my RH Linux box. domaincentral have been great (I’ve now got 3 domains with them). www.puterpet.com – this one www.techie-blogs.com and darlingranges.com (still setting this up).
7) Setup port forwarding on broadband router, then fire up named and it was all good. Setup up virtual hosts (www.puterpet.com and mail.puterpet.com) in that domain. Now can setup any mail account or host in that domain, it is just beautiful! 🙂
8) Installed/updated sendmail. Especially like watching sendmail’s incoming and outgoing messages. 🙂
9) Edit /etc/mail/sendmail.mc – which is a macro config file that gets fed into m4 (macro language). Had to update m4, etc as kept breaking during conversion.
10) Edited various other files under /etc/mail – such as relay_domains (allow email from puterpet.com, etc); aliases …
12) Installed dovecot – which services POP and SMTP requests.
13) Created some accounts to handle mail – sales; enquiries, etc.
14) Modified my apache confs to handle and redirectmatch to new domain.

[/solution]

[example]

[/example]

[reference]

[tags]sendmail, DNS, POP, SMTP, Red Hat Linux, Unix Coding School[/tags]

[/reference]

Ping host measured in microseconds

[problem]

You want to ping a host on a given port and record time taken in microseconds.

The following solution uses PHP socket libraries to open a connection, to the given host and port – then just close it again. By obtaining the microseconds before and after, it is able to deduce time taken in microseconds.

Additionally I’ve added functionality to print the epoch, so the timestamp can be obtained.

[/problem]

[solution]

pingStat.php


<?php
list($usec, $sec)=explode(" ",microtime());
$thisDate=sprintf("%f", ($usec+$sec));
$host="$argv[1]";
$port="$argv[2]";

if(!$host) {
die("Usage: $0 host port [ dtg ]n");
}

$fp = fsockopen("$host", $port, $errno, $errstr, 30);
if (!$fp) {

print("ping failed to host: $host:$port -
$errstr ($errno)n");

} else {

list($usec, $sec)=explode(" ",microtime());
$nextDate=sprintf("%f", ($usec+$sec));

if($argv[3]) {
printf("%d:%0.4fn",time(),$nextDate-$thisDate);
} else {
printf("%0.4fn",$nextDate-$thisDate);
}

fclose($fp);

}

?>

[/solution]

[example]

First example pings a host on port 80 and prints the current epoch.


$ php -q ./pingStat.php apachehost 80 y
1183524570:0.0824

Second example pings it again on port 443 and this time does not print epoch.


$ php -q ./pingStat.php apachehost 443
0.0775

[/example]

[reference]

[tags]Ping host measured in microseconds, PHP Coding School[/tags]

[/reference]