กระทู้เมื่อเร็วๆ นี้

หน้า: [1] 2 3
1
Linux Commandline. / find mtime older than 100 days and then move to folder archive
« กระทู้ล่าสุด โดย admin เมื่อ กุมภาพันธ์ 27, 2015, 11:21:24 AM »
find -type f -mtime +100  -exec mv -v '{}' archive/ \;
2
Linux Shell Scripts / awk convert unix timestamp to date time
« กระทู้ล่าสุด โดย admin เมื่อ กุมภาพันธ์ 27, 2015, 11:13:49 AM »
some Service log use time stamp . Easy convert to standard date time

Example Nagios log :
tail -f /usr/local/nagios/var/nagios.log
[1425007447] SERVICE ALERT: PROB_API;check_ais_rbt_server_responce;OK;HARD;3;OK. Found 0

awk 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S",1425008707); }'     
   
2015-02-27 10:45:07

Success.
3
Linux Server install and setup. / safe-rm : Protect important file and folder with rm command
« กระทู้ล่าสุด โดย admin เมื่อ กุมภาพันธ์ 24, 2015, 05:55:23 PM »
TAG : safe-rm   
Protect important file and folder with rm command

cd /root/installed/
wget http://linux.thaieasydns.com/downloads/safe-rm-0.9.tar.gz

tar -zxvf safe-rm-0.9.tar.gz
cd safe-rm-0.9
cp safe-rm /usr/local/bin/
cd /usr/local/bin/
ln -s safe-rm rm

create file config /etc/safe-rm.conf
vim /etc/safe-rm.conf

input line below :
/root/test/test

:wq!

test delete from list above :

rm -rf /root/test/test
safe-rm: skipping /root/test/test

comment : if protect folder /root/test 
config on safe-rm.conf 

/root/test   !!!! not use /root/test/   
if protect all file in /root/test/  use /root/test/* but protect sub file on folder /root/test/xxx/*

rm -rf /root/test/
safe-rm: skipping /root/test/

rm -rf /root/test
safe-rm: skipping /root/test

Success...
4
Linux Shell Scripts / Replace all occurences of spaces in filenames with an underscore character
« กระทู้ล่าสุด โดย admin เมื่อ กุมภาพันธ์ 17, 2015, 01:17:48 PM »
Create file : no_spacebar.sh
vim no_spacebar.sh 

input line below :

โค๊ด: [Select]
#!/bin/sh
#
# (Hopefully) replace all occurences of spaces in filenames with an underscore character
#
# Expected Usage: killspc [regexp]
# regexp represents location and types of files to rename i.e $HOME/downloads/*
#

TOTAL_RENAMED=0
for NXT_FILE in "$@" ; do
        case "$NXT_FILE" in *' '*)
        OLD_NAME="$NXT_FILE"
        NEW_NAME=`echo "$OLD_NAME" | tr ' ' '_'`
        echo -e "Renaming file `basename "$OLD_NAME"` to `basename $NEW_NAME`...\c"
        OLDIFS="$IFS"
        IFS=:
        mv "$OLD_NAME" "$NEW_NAME"
        IFS="$OLDIFS"
        if [ $? -eq 0 ]; then
                echo "ok"
                TOTAL_RENAMED=$(( $TOTAL_RENAMED +1 ))
        else
                echo "failed"
        fi
        ;;
        esac
done

change permission file to 755
chmod 755 no_spacebar.sh 

and copy file no_spacebar.sh   to destination folder

Before :
ls -la
total 12

-rwx------ 1 root root  657 Mar 26 12:53 no_spacebar.sh
-rw-r--r-- 1 root root    0 Mar 26 12:50 test2 test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test3 test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test4 test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test5 test
-rw-r--r-- 1 root root    0 Mar 26 12:49 test test


./no_spacebar.sh test*

after :

ls -al
โค๊ด: [Select]
-rwx------ 1 root root  658 Mar 26 12:56 no_spacebar.sh
-rw-r--r-- 1 root root    0 Mar 26 12:50 test2_test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test3_test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test4_test
-rw-r--r-- 1 root root    0 Mar 26 12:50 test5_test
-rw-r--r-- 1 root root    0 Mar 26 12:49 test_test


How to use : Copy file no_spacebar.sh to destination folder and run

 
5
Linux Server install and setup. / How To Install Nginx as a Reverse Proxy for Apache Web Server
« กระทู้ล่าสุด โดย admin เมื่อ สิงหาคม 18, 2014, 06:21:38 PM »
Nginx(fontend) as a reverse Proxy for Apache(Backend)

Before Apply Solution
โค๊ด: [Select]
client --http port tcp 80 -->  Apache Web Server 

After Apply Solution
โค๊ด: [Select]
client --http port tcp 80 --> Nginx - http port tcp 8080 ->  Apache Web Server
Server OS :Centos

- Install Apache
#yum install httpd httpd-devel -y

Configure the Reverse Proxy settings on Apache
#vim /etc/httpd/conf/httpd.conf

โค๊ด: [Select]
Listen 80
to
Listen 8080

โค๊ด: [Select]
NameVirtualHost *:80
to
NameVirtualHost *:8080

<VirtualHost *:8080>
   ServerName www.yourdomain.com
   ServerAlias yourdomain.com
   DocumentRoot /var/www/yourdomain.com
       <Directory "/var/www/yourdomain.com">
               Options FollowSymLinks -Includes
               AllowOverride All
               Order allow,deny
               Allow from all
       </Directory>
       RewriteEngine on

</VirtualHost>

#/etc/init.d/httpd restart



Install Nginx with yum
#yum install nginx -y

#vim /etc/nginx/nginx.conf

โค๊ด: [Select]
user nginx;
worker_processes  4;
error_log  logs/error.log crit;
#error_log  logs/error.log info;

# set open fd limit to 30000
worker_rlimit_nofile 30000;

events {
worker_connections  8192; # you might need to increase this setting for busy servers
multi_accept on;
use epoll; #  Linux kernels 2.6.x change to epoll
}

http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;

server_tokens off;

include    mime.types;
default_type  application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout  10;

# Gzip on
gzip on;
gzip_min_length  1100;
gzip_buffers  4 32k;
gzip_types    text/plain application/x-javascript text/xml text/css;

# Other configurations
ignore_invalid_headers on;
client_max_body_size    8m;
client_header_timeout  3m;
client_body_timeout 3m;
send_timeout     3m;
connection_pool_size  256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size  4k;
output_buffers   4 32k;
postpone_output  1460;

# Cache most accessed static files
open_file_cache          max=10000 inactive=10m;
open_file_cache_valid    2m;
open_file_cache_min_uses 1;
open_file_cache_errors   on;

# virtual hosts includes
include "/etc/nginx/conf.d/*.conf";

}

and create virtual host on nginx

/etc/nginx/conf.d/yourdomainname.com.conf

โค๊ด: [Select]
server {
        listen    80;
        server_name  www.yourdomainname.com yourdomainname.com;
        access_log off;
        error_log  logs/www.yourdomainname.com-error_log crit;

location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
        root /var/www/yourdomainname.com;
        expires 365d;
        }

location / {
        client_max_body_size    10m;
        client_body_buffer_size 128k;

        proxy_send_timeout   90;
        proxy_read_timeout   90;
        proxy_buffer_size    128k;
        proxy_buffers     4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_connect_timeout 30s;

        proxy_redirect  http://www.yourdomainname.com:8080   http://www.yourdomainname.com;
        proxy_redirect  http://yourdomainname.com:8080   http://yourdomainname.com;
       

        proxy_pass   http://127.0.0.1:8080/;

        proxy_set_header   Host   $host;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}
Install mod_rpaf
mod_rpaf will help Apache to know who connects to it (otherwise the only IP address you will see in your logs is 127.0.0.1):

mkdir /root/installed/
cd /root/installed/
wget https://github.com/y-ken/mod_rpaf/archive/master.zip
unzip master.zip
cd mod_rpaf-master/
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

#vim /etc/httpd/conf.d/rpaf.conf

โค๊ด: [Select]
LoadModule rpaf_module /usr/lib64/httpd/modules/mod_rpaf-2.0.so

#Reverse proxy
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 Your_Real_Server_IP
#/etc/init.d/httpd restart

curl -I http://www.yourdomainname.com/

output :

HTTP/1.1 200 OK
Server: nginx

Complete!!!!!

6
Linux Shell Scripts / Shell Scripts Check Service httpd and auto restart service httpd when it down.
« กระทู้ล่าสุด โดย admin เมื่อ มีนาคม 06, 2013, 11:40:40 AM »
TAG : Shell Scripts Check Service httpd and auto restart service httpd when it down.

#create shell scripts file
vi /root/check_httpd.sh
input line below :

#!/bin/sh
/usr/bin/wget --tries=1 --timeout=30 -O /dev/null http://localhost/check_httpd.txt

if [ $? -ne 0 ]; then
(
/etc/init.d/httpd restart
datetime=`date "+%Y%m%d %H:%M:%S"`
echo 'restarting httpd' `ps aux` | mail -s "`hostname` restarting httpd `who | awk '{print $6}'`" your_email@yourdomain.com
datetime=`date "+%Y%m%d %H:%M:%S"`
echo $datetime "failure">>/var/log/httpd/check.log
)
datetime=`date "+%Y%m%d %H:%M:%S"`
echo $datetime "Normal" >> /var/log/httpd/check.log
fi

:wq!  save and quit

#Create page for check  on DocumentRoot  Example DocumentRoot = /var/www/html/
touch /var/www/html/check_httpd.txt

#Auto Check with crontab check every time 5 minutes
crontab -e
input line below :
*/5 * * * *  /bin/sh /root/check_httpd.sh 1> /dev/null 2> /tmp/checkhttpd

:wq! save and quit

Success!!!
7
Linux Commandline. / Command line Check Duplicate Number on file
« กระทู้ล่าสุด โดย admin เมื่อ มีนาคม 06, 2013, 11:31:34 AM »
TAG : Command line Check Duplicate Number on file

Example : if you need to check dulicate number on filename test.txt  and send output to filename duplicate-test.txt

cat test.txt   

[
โค๊ด: [Select]
color=red]1111[/color]
[color=red]2222[/color]
3333
4444
5555
[color=red]1111
1111[/color]
[color=red]2222[/color]

cat test.txt | rev | cut -f1 -d/ | rev | sort | uniq -c |  expand | grep -v ' 1 ' > duplicate-test.txt

cat duplicate-test.txt
     
โค๊ด: [Select]
3 1111                     
      2 2222
#Meaning line above 
โค๊ด: [Select]
    3 1111           -->     1111 Duplicate 3 line     
    2 2222           -->     2222 Duplicate 2 line


Success!!!!
[/size]
8
Linux Shell Scripts / Shell Scripts Delete File on Folder older than 30 Days
« กระทู้ล่าสุด โดย admin เมื่อ มีนาคม 06, 2013, 11:24:32 AM »
TAG : Shell Scripts Delete File on Folder older than 30 Days

Example : if you need to delete file older than 30 days ago on folder /home/test/

#Create file shell scripts
vi /root/delete-file-30days-ago.sh
input line below :

โค๊ด: [Select]
#! /bin/sh
find /home/test/ -type f -mtime +30 -exec rm -f {} \; >> /root/log-delete-file-30days-ago.log

:wq! save and quit

#permission shell scripts allow root only can Read-Write-Execute
chmod 700 /root/delete-file-30days-ago.sh

#command line Manual Run Shell Scripts one time.
sh /root/delete-file-30days-ago.sh

#on running scripts above you can check realtime log delete with command below:
tail -f /root/log-delete-file-30days-ago.log

exit tail -f  with Ctl+c

Success!!![/size]
9
Linux Shell Scripts / Auto Backup Crontab User root Every Weeks time 04:00 a.m.
« กระทู้ล่าสุด โดย admin เมื่อ มีนาคม 06, 2013, 11:17:20 AM »
TAG: Auto Backup Crontab User root Every Weeks time 04:00 a.m.

#Create Folder For Backup
mkdir /root/backup/

#Create Folder For Shell Scripts
mkdir /root/scripts/

#Create Shell Scripts File
vi /root/scripts/backup-crontab.sh

#!/bin/bash
crontab -l -u root > /root/backup/crontab-`date +%Y-%m-%d`

:wq! Save and Quit

#Chmod Permission file for Shell Scripts Allow User root - Read+Write+Execute
chmod 700 /root/scripts/backup-crontab.sh


#Add Shell Scripts Run Every Weeks time 04:00 a.m. on crontab

crontab -e

input line below :
#backup crontab every weeks
0 4 * * 0 /bin/sh /root/scripts/backup-crontab.sh

:wq! = save and quit

Success!!!!
10
Linux Server install and setup. / HA Proxy with session support
« กระทู้ล่าสุด โดย admin เมื่อ มีนาคม 03, 2013, 10:21:33 PM »
TAG : how to install HA Proxy with session support

example  :  HA Proxy user internet  access to http://10.1.228.137/  round robin to Web1 and Web2  with web server  session support

โค๊ด: [Select]
HAProxy: 10.1.228.137 port 80

Web1 : 10.1.228.52 port 80

Web2 : 10.1.228.53 port 80

install haproxy
wget http://blog.up2box.com/downloads/haproxy-1.4.20.tar.gz
Compile the sources with `make install`
#tar -zxvf  haproxy-1.4.20.tar.gz

#cd haproxy-1.4.20

#make install

#cp haproxy /usr/sbin/haproxy

+Download a sample config file
#wget http://blog.up2box.com/downloads/haproxy-standard.cfg -O /etc/haproxy.cfg
+Download a launcher
#wget http://blog.up2box.com/downloads/haproxy.init -O /etc/init.d/haproxy
+adjust the startup settings
#chmod +x /etc/init.d/haproxy
#chkconfig --add haproxy
#chkconfig haproxy on

vim /etc/haproxy.cfg

input line below :

โค๊ด: [Select]
listen webfarm 10.1.228.137:80
mode http
balance source
cookie JSESSIONID prefix
option httpchk HEAD /check.txt HTTP/1.0
option httpclose
option forwardfor
server web1 10.1.228.52:80 cookie A check
server web2 10.1.228.53:80 cookie B check
installed success[/size]
หน้า: [1] 2 3