Unix File System-filled cleanup policies

Source: Internet
Author: User
Tags dmesg
Article Title: unix File System full cleaning policy. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Although the current disk capacity is getting bigger and bigger, it is likely to be full after all. In case of a careless system administrator, the time to fill the disk will be shorter. In a unix/linux operating environment, once a partition (also called a file system) is filled up, the consequence may be very bad-I had an experience working at AM-the partition/tmp is full, resulting in a daemon process being unable to write data to the disk and terminating unexpectedly. Presumably other people have similar situations. How can we deal with and avoid such troubles? Here are some comments for your reference.

First, let's talk about non-technical factors. Simply put, we will talk about rules and regulations. Linux/unix is mostly a public server and private data unrelated to work should be prohibited from being uploaded. A Jun bought a NAS (network attached storage) device and four GB hard disks. He was planning to store web Background data, .... Later, as far as I know, this large-capacity disk has less than 20 GB space in less than two months and is browsed privately. Hey! Most of the data is his private, and he had the hobby of collecting waste products. No wonder. In this regard, the system should be more rigorous to avoid colleagues from putting junk data in public spaces.

Data on the disk may grow at any time, and no one can stare at it 24 hours a day. Therefore, automatic monitoring is necessary. For a larger network environment, this may be the only way. The following is a script using perl to monitor disk capacity (Daewoo contributes ):

#! /Usr/bin/perl-w

# This program will check disk capacity $ full and send the warning message

# To $ email_address

# (Set the threshold to 90 and check it in the daytime so no paging

# Is needed)

My $ email_address = 'sa @ yourcom.com ';

My $ hostname = '/sbin/ifconfig-a | grep inet | head-1 | cut-f2-d ":" | cut-f1-d ""';

My $ dmesg = 'dmesg ';

Chomp (my $ now = 'date + "% x % X "');

My $ full = 90; # the threshold to send the warning

My $ warn = 95;

My $ count = 0;

My ($ dev, $ total, $ used );

My @ df_messages = 'df | grep-v proc ';

Print @ df_messages;

Shift (@ df_messages );

Foreach $ message (@ df_messages ){

Chomp ($ message );

($ Dev, $ total, $ used, $ available, $ capacity, $ mount) = split (/\ s +/, $ message );

$ Capacity = ~ S/(\ d +) \ %/$1 /;

If ($ capacity> $ full ){

$ Available [$ count] = $ available;

$ Capacity [$ count] = $ capacity;

$ Mount [$ count] = $ mount;

+ + $ Count;

$ Email_address = 'sa @ yourcom.com 'if ($ capacity> $ warn );

}

}

If ($ count> 0 ){

Open (MAIL, "|/usr/sbin/sendmail-t ");

Print MAIL "To: $ email_address \ n ";

Print MAIL "Subject: Disk almost full on $ hostname ($ now) \ n ";

Print MAIL "\ n ";

For ($ I = 0; $ I <$ count; ++ $ I ){

Print MAIL "There are only $ available [$ I] KB ($ capacity [$ I] \ % full) left on $ mount [$ I] \ n ";

}

}

If ($ dmesg = ~ M/ERROR /)

{

Open (EMAIL, "|/usr/sbin/sendmail-t") or die "Can't fork for sendmail: $! \ N ";

Print EMAIL <_ EOF _;

To: $ email_address

Subject: HARDWARE error on $ hostname !!!

$ Hostname needs to be checked right now!

.

_ EOF _

Close ("EMAIL ");

}

Put this script in the crontab of the scheduled task for automatic monitoring. As long as the capacity of a partition reaches the threshold value in the script, the system will send an alarm email to the Administrator mailbox, in addition, you can set to send SMS alarms.

When you know that a partition is about to be filled up, the next thing is to clear it. Log on to the system and run the df command? H. Check the disk usage. (the option-h is not supported in earlier versions of solaris. Use the option-k ),

The disk usage is displayed as a percentage, which is very intuitive. After finding a fully occupied partition, you should first find the largest file that occupies the largest space, and then process the file that occupies the largest space. Here I use an instance (root partition/root) to demonstrate this process.

1. Enter the directory/root and execute the command du? H | sort? N, the Directory and the size of the files in the current directory are arranged in order. If a screen is not displayed, add a pipeline du? H | sort? N | more.

-Bash-3.00 # du-h | sort-n | more

1 K./. dt/appmanager

1 K./. dt/help

1 K./. dt/icons

1 K./. dt/tmp

.......... (Omitted rows)

914 K./mysql-5.0.37/zlib

933 K./mysql-5.0.37/ndb/src/kernel/blocks/dblqh

938 K./mysql-5.0.37/scripts

945 M. // This Stuff occupies too much space

957 K./mysql-5.0.37/extra/yassl/taocrypt

959 K./vsftpd-2.0.5

1002 K./mysql-5.0.37/ndb/src/common

-Bash-3.00 #

With the above output, we can know that there are large files in the current directory, but we cannot see which file is used.

2. Run the ls? Al | grep ^-| more to view the size of each file.

-Bash-3.00 # ls-al | grep ^-| more

-Rw ------- 1 root 810 Apr 29. ICEauthority

-Rw ------- 1 root 98 Apr 29 09: 59. Xauthority

-Rw ------- 1 root 730 Apr 30. bash_history

-Rwxr-xr-x 1 root 5111 Apr 29. dtprofile

-Rw-r -- 1 root 81 Apr 29. gtkrc-1.2-gnome2

-Rw ------- 1 root 0 Apr 29 08:30. recently-used

-Rw-r -- 1 root 681090961 Feb 28 :29 10202_database_solx

86. zip

.......... (Omitted)

-Rw-r -- 1 root 3069440 Apr 29 tar-1.16-sol10-x86-lo

Cal

-Rw-r -- 1 root 10895360 Oct 22 2006 tar-1.16.tar

-Rw-r -- 1 root 155985 Jul 3 2006 vsftpd-2.0.5.tar.gz

-Bash-3.00 #

Which of the following statements is red? The file name is 10202_database_solx86.zip, and then run the command du? H 10202_database_solx86.zip: the size of the database is 650 MB.

-Bash-3.00 # du-h 10202_database_solx86.zip

650 M 10202_database_solx86.zip

3. Remove or delete large files that occupy space.

Let's take a look. Is it easy to find a large file ?! Of course, it is more convenient to use tools such as awk to write shell scripts. Is there another way to use find to add options? Size, please try it yourself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.