Get Social

Bash script in one line to check free space in Linux

This task arose after the client sites were moved from the old VPS to the new one. On the new virtual server, everything is fine with the processor and the RAM, but the disk space is very close. It turned out that Vesta CP, which by default creates 3 backups, just filled the disk to zero.

The client asked me that when the place becomes small – it immediately became known. So I decided to make a very simple script “in one line”, which tracks disk space and sends a notification to e-mail. Such a script can be completely inserted in the Cron scheduler.

Actually, here it is:

Linux disk space monitoring script


if [ "`df | grep "/dev/sda1" | awk '{print $5}' | sed 's/\%//'`" -ge 95 ]; then echo "Disk usage exceeded 95%" | mail -s "Warning! My Server" mail@example.com; fi

When the disk usage reaches 95%, this one-line script will start sending messages with warnings to the mail “mail@example.com”. For example, you can add this line to the cron to check every 45 minutes. Of course, you can set it for every minute, but then your mailbox will just be fill up with these messages.

You can also go ahead and do so that we have one more warning when you reach 99%:

if [ "`df | grep "/dev/sda1" | awk '{print $5}' | sed 's/\%//'`" -ge 99 ]; then echo "Disk usage exceeded 99%!" | mail -s "Panic!! My Server" mail@example.com; fi

The situation is more serious here, so you can put in the cron to check every 5 minutes

As a result, we get in crontab:

# server usage alert
*/45 * * * * if [ "`df | grep "/dev/sda1" | awk '{print $5}' | sed 's/\%//'`" -ge 95 ]; then echo "Disk usage exceeded 95%" | mail -s "Warning! My Server" mail@example.com; fi
*/5 * * * * if [ "`df | grep "/dev/sda1" | awk '{print $5}' | sed 's/\%//'`" -ge 99 ]; then echo "Disk usage exceeded 99%!" | mail -s "Panic!! My Server" mail@example.com; fi

Of course, you can go even further and make these scripts to one script in a separate file and with a nice syntax and indentations. But the above option just works and it’s enough for me.

P.S. Make sure that you have the correct disk (“/dev/sda1” or something else) in the output of the df command.

Installing and configuring sphinx on centos 5

This information is suitable for sphinx versions 2.0.9 and below (also sphinx 1.x).
If you need to install and configure version 2.2.x (2.2.11, for example), then see here >>

[1] How to install sphinx on centos 5

Where to search for information?

As always – first we look, that is in search on query “installation sphinx on centos”.
We open several tabs and work on it. And of course, we look at the official documentation (http://sphinxsearch.com/docs)
yum search sphinx
yum install sphinx
cat /etc/redhat-release
CentOS release 5.9 (Final)

wget http://sphinxsearch.com/files/sphinx-2.0.9-1.rhel5.i386.rpm
yum --nogpgcheck localinstall sphinx-2.0.9-1.rhel5.i386.rpm

rpm -Uvh sphinx-2.0.9-1.rhel5.i386.rpm
error: Unsatisfied dependencies:
libmysqlclient.so.15 required for sphinx-2.0.9-1.rhel5.i386
libmysqlclient.so.15(libmysqlclient_15) required for sphinx-2.0.9-1.rhel5.i386

I go to http://sphinxsearch.com/docs/current.html#installing
wget http://sphinxsearch.com/files/sphinx-2.0.9-release.tar.gz

cd sphinx-2.0.9-release
./configure
make

libsphinx.a(sphinx.o): In function `xmlUnknownEncoding':
/root/install/sphinx-2.0.9-release/src/sphinx.cpp:22846: undefined reference to `libiconv_open'
/root/install/sphinx-2.0.9-release/src/sphinx.cpp:22864: undefined reference to `libiconv'
/root/install/sphinx-2.0.9-release/src/sphinx.cpp:22870: undefined reference to `libiconv_close'
collect2: ld completed with a return code 1

How to fix this error “collect2: ld completed with a return code 1” ?

replace:
LIBS = -lexpat -ldl -lm -lz -L/usr/local/lib -lrt -lpthread
to
LIBS = -lexpat -liconv -ldl -lm -lz -L/usr/local/lib -lrt -lpthread
profit!

make
make install

[2] How to configure sphinx on centos 5

just in case, backup the original configs:
cd /usr/local/etc
cp -i sphinx-min.conf.dist sphinx-min.conf.dist.copy
cp -i sphinx.conf sphinx.conf.copy

set the configuration for own database
nano /usr/local/etc/sphinx.conf

We will create the folders listed in the config (otherwise we’ll get errors … 🙂 )
mkdir /home/user/sphinx_data/
mkdir /var/log/sphinx/

start service:
searchd --config /usr/local/etc/sphinx.conf

check that the service on the required ports is working
lsof -i tcp:9312
lsof -i tcp:9306

check that the log files have been created:
ls -lh /var/log/sphinx/

Useful commands for sphinx search service

create index:
indexer --config /usr/local/etc/sphinx.conf --all

update index:
indexer --config /usr/local/etc/sphinx.conf --rotate

search test in the console:
search -q --config /usr/local/etc/sphinx.conf -a 123
or simple:
search -q 123
In order to use the search on the site, you need to use the Sphinx API for PHP.
This is the library that comes with the distribution. We find where it is:
locate sphinxapi.php
<?php
.....
// Plug sphinx-api
require_once ("sphinxapi.php");
.....
?>

Add cron task to the rotation every 15 minutes (the indexer path is full, because the cron are usually do not know where to look for executable files):
*/15 * * * * /usr/local/bin/indexer --config /usr/local/etc/sphinx.conf --rotate > /dev/null 2>&1

[3] How to make searchd (sphinx) start on reboot?

1) Beautiful method – as described in this article.

2) A simple method is to check in the cron every minute whether searchd is started and run it, if necessary. Criterion – check the existence of a pid-file:
* * * * * if [ ! -f /var/log/sphinx/searchd.pid ]; then /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf; fi > /dev/null 2>&1

 

Update, 21.12.2016: This settings for Sphinx 1.x и 2.0.x; to learn how to install and configure Sphinx versions 2.2.x read: Installing and Configuring Sphinx 2.2.11 (Yii with delta Indexes) on Ubuntu / Debian and RHEL / CentOS 7

How to encrypt a backup and upload it on ftp or sftp?

If you need to upload backup to a third-party ftp-server (archive, just a file), however, you need to ensure that important information does not fall into the wrong hands, before uploading, encrypt it with a password and then upload it.

We encrypt the file with the openssl command

openssl aes-256-cbc -salt -in FILE -out FILE.aes -k PASSWORD
(openssl there are in most Linux distributions, and you can also install it in windows and mac os)
To request a password, you need to remove the “-k PASSWORD”.
You can decrypt with the command
openssl aes-256-cbc -d -salt -in FILE.aes -out FILE -k PASSWORD

 

How to upload to ftp (using curl best)

curl -s -T FILE -u USER:PASSWORD ftp://ftpserverhostname

 

How to upload to sftp

rsync -avz -e ssh USER@PASSWORD:/remote/directory/ /current/directory/file

 

How to upload to sftp with curl

short answer – use rsync over ssh and do not suffer 🙂

Pages:12