Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Ram Usage Per Application

Hi all,
I found a cool script that show you very nicely how many ram memory every application on your server used:

444.0 KiB +   0.0 KiB = 444.0 KiB       atd
536.0 KiB +   0.0 KiB = 536.0 KiB       portmap
620.0 KiB +   0.0 KiB = 620.0 KiB       ping
640.0 KiB +   0.0 KiB = 640.0 KiB       acpid
748.0 KiB +   0.0 KiB = 748.0 KiB       init
764.0 KiB +   0.0 KiB = 764.0 KiB       rpc.statd
788.0 KiB +   0.0 KiB = 788.0 KiB       dhcdbd
904.0 KiB +   0.0 KiB = 904.0 KiB       check_ping
956.0 KiB +   0.0 KiB = 956.0 KiB       xinetd
960.0 KiB +   0.0 KiB = 960.0 KiB       hald-addon-acpi
  1.0 MiB +   0.0 KiB =   1.0 MiB       cron
  1.0 MiB +   0.0 KiB =   1.0 MiB       dbus-daemon
  1.1 MiB +   0.0 KiB =   1.1 MiB       system-tools-ba
  1.1 MiB +   0.0 KiB =   1.1 MiB       hald-addon-inpu
  1.1 MiB +   0.0 KiB =   1.1 MiB       hald-runner
  1.2 MiB +   0.0 KiB =   1.2 MiB       kerneloops
  1.2 MiB +   0.0 KiB =   1.2 MiB       NetworkManagerD
  2.0 MiB +   0.0 KiB =   2.0 MiB       rsyslogd
  2.0 MiB +   0.0 KiB =   2.0 MiB       avahi-daemon (2)
  2.1 MiB +   0.0 KiB =   2.1 MiB       NetworkManager
  2.2 MiB +   0.0 KiB =   2.2 MiB       pickup
  2.2 MiB +   0.0 KiB =   2.2 MiB       master
  2.2 MiB +   0.0 KiB =   2.2 MiB       nagios (2)
  2.3 MiB +   0.0 KiB =   2.3 MiB       hald-addon-stor (2)
  2.3 MiB +   0.0 KiB =   2.3 MiB       cupsd
  2.3 MiB +   0.0 KiB =   2.3 MiB       udevd
  2.4 MiB +   0.0 KiB =   2.4 MiB       qmgr
  3.0 MiB +   0.0 KiB =   3.0 MiB       screen (2)
  3.0 MiB +   0.0 KiB =   3.0 MiB       hald
  3.4 MiB +   0.0 KiB =   3.4 MiB       getty (6)
  4.2 MiB +   0.0 KiB =   4.2 MiB       sshd (2)
  4.6 MiB +   0.0 KiB =   4.6 MiB       gdm (2)
  5.3 MiB +   0.0 KiB =   5.3 MiB       snmpd
  6.2 MiB +   0.0 KiB =   6.2 MiB       Xorg
  7.0 MiB +   0.0 KiB =   7.0 MiB       munin-node
  9.0 MiB +   0.0 KiB =   9.0 MiB       collect2.pl
 17.6 MiB +   0.0 KiB =  17.6 MiB       bash (5)
 25.6 MiB +   0.0 KiB =  25.6 MiB       gdmgreeter
120.5 MiB +   0.0 KiB = 120.5 MiB       apache2 (11)

 Private  +  Shared  =  RAM used        Program

download the script from HERE - ps-mem.pl
to run the script to need to type:
# python ./ps_mem.pl




Enjoy (-:

Please leave a comment if you like this post.
Read more >>

How To, MySQL to CSV - Script

Hi
I wanted to share a new script that I was writing,
This script knows how to take SQL queries, convert them to csv files and send them by eMail.

the script is divided to 2 files:
the first one contain all the sql queries, and the second do all the amazing job.

The fist file called SQL.reports can be download from HERE, contain the sql queries, the syntax must be like the example:
ReportName your_report_name
sql your query

ReportName your_second_report_name
sql select * from tableA where [...]

The script file called MySQL_to_csv.sh can be download from HERE,
the only things you need to do for this to work is to fill the next parameters in the script:
DBhost="localhost"
DBname="your DB name"
DBuser="your DB user name"
DBpass="your DB password"
Mail_to='list of mail with space between them'
please do not touch the rest of the script.

After you have been download those 2 file, placed them in the same directory on your server lets say /opt/reports
and make sure you give them a permission
# chmod +x /opt/reports/*

now all left to do is to run the MySQL_to_csv.sh script
# /opt/reports/MySQL_to_csv.sh
and check you mailBox.
you can also add the script to corntab to run in your own scheduling

Enjoy.
And please comment (-;
Read more >>

How To sort and find duplicate records

Hi,
I'm was asked to sort a password file, to show all duplicate users and their UIDs.
For Example if you have a file like this:
UID    UserName   Password
----   --------   ----------
1236   john       oe93kf9034j
936    max        fkl03kf032j
9381   keren      fg38uf6124t
8988   john       fj3589gjf01
9834   roma       fgj390jf203
8915   max        gf23j09g305
341    john       mf9244t24t4
9841   david      f02jflp3053
43745  david      23hkfg03g35
4956   ron        04fk054f2e4
69595  max        kvf9035g022
7765   john       mg30gk30gkw

I want to show all the duplicate users and all their UIDs like this:
john - 1236 8988 341 7765
max - 936 8915 69595
david - 9841 43745

Solution
To find all duplicate users we'll use the next script:
$ cut -f2 yourFile | sort | uniq -d
it'll display:
john
max
david

Now we want to know all the UIDs of those users
create a new file with vi
$ vi dup_users.sh
and copy the folloing lines:
dup=`cut -f2 yourFile | sort | uniq -d`

for a in $dup
do
        echo $a - `cat yourFlie | grep $a | cut -f1`
done

It will search for each user all UIDs he has and display them like the example on the top.

More
$ sort yourFlie | uniq -u # only the unique, non-repeated lines
$ sort yourFile | uniq -d # repeated lines
$ sort yourFile | uniq -c # all lines + count repeated lines


Read more >>

How to add FTP user - script

- After installing FTP server -
check my HowTo post - How to install FTP server.
you want to get the next script for add your FTP users easily.

Create a new script:
# vi /usr/bin/add_ftp_user
and copy the next lines to it
#!/bin/bash
# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

usermod -g proftpd $username
save and run the command:
# chmod +x /usr/bin/add_ftp_user

For add a new user just run the script
# add_ftp_user
enter a user name and password
and that's it.

Enjoy


Read more >>

How to tweet from command line

First, make sure you have "curl" install on your machine.
if not, install it with
"yum" or "apt-get".










Create a new script with your favorite editor, i like vi
# vi /usr/bin/tweet (as root user)
and copy the next lines to it.

#!/bin/sh

tweet="${@}"
user="USER"
pass="PASSWORD"

if [ $(echo "${tweet}" | wc -c) -gt 140 ]; then
echo "FATAL: The tweet is longer than 140 characters!"
exit 1
fi

curl -k -u ${user}:${pass} -d status="${tweet}" https://twitter.com/statuses/update.xml >/dev/null 2>&1

if [ $? == "0" ]; then
echo "Successful tweet!"
fi

Change the USER to your user name and the PASSWORD to your twitter password.
save and run the command:
# chmod +x /usr/bin/tweet

that's it.

run from your command line
# tweet "Testing tweet sctipt"

and you are twitting.

Read more >>

How to delete files, except the recent one

Hi,
I am using this script to delete logs file from my server.
It search all the files in the certain folder and delete them all except the recent one.
this help me to save disk space on my servers.


#!/bin/sh

DIR=/any/dir/you/want

cd $DIR
lastfile=$(ls -rt | tail -1)
for afile in $(ls | grep -v $lastfile)
do
echo > $afile
rm -rf $afile
done

Enjoy.


Read more >>

How to use ftp in a shell script

If you want to send a file to FTP server for backup or for any other reason,
create new script file with your favorite editor
# vi /usr/bin/ftp_put.sh
and copy the next lines to it

#!/bin/sh
HOST='ftp server'
USER='user name'
PASSWD='password'
FILE='$1'

ftp -n $HOST
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

change the "ftp server", "user name" and "password" to your needs.
after saving the file, run the next command
# chmod +x /usr/bin/ftp_put.sh

That's it
run this command to send file to your ftp server
# ftp_put.sh /any/file/you/want


Read more >>