Software RAID Setup under Ubuntu

December 3rd, 2008

install ‘mdadm’. to reconfig run ‘dpkg-reconfigure mdadm’

CREATE THE ARRAY

raid5: mdadm - -create - -force /dev/md0 - -level=raid5 - -chunk=64 - -parity=left-symmetric - -raid-devices=3 /dev/sdb /dev/sdc /dev/sdd

raiad0/Striping:  mdadm - -create /dev/md0 - -level=stripe - -chunk=4096 - -raid-devices=2 /dev/sda /dev/sdb

STOP MDADM
sudo mdadm –stop /dev/md0 (if resource busy)

CREATE PARTITION
sudo fdisk /dev/md0

FORMAT
sudo mkfs.ext3 /dev/md0

MOUNT DRIVE
mount /dev/md0 /mnt/raid

VIEW RAID DETAILS

mdadm -D /dev/md0

-add to /etc/fstab for mount on boot!
-do a ‘df’ to view your new system!

SAVE CONFIG FOR REBOOT

mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Uncategorized

Python remove duplicate files script (based on md5 hash)

December 2nd, 2008
#!/usr/bin/python
# This script compares the file contents of two directories
# using and md5 hash and deletes any duplicate files from the
# target directory

import os
import md5
import sys
import base64

source={}
target={}

source_path="/home/jesterj/source"
target_path="/home/jesterj/target"

def gather_files(dir,dict):
	os.chdir(dir)
	names=os.listdir(dir)
	for filename in names:
		#read file
		f = open(filename)
		file_contents = f.read()

		#create new hash
		hash=md5.new()
		hash.update(file_contents)
		hex=hash.hexdigest()

		#add to dictionary
		dict[hex] = filename

def remove_matches():
	has_dupes=0
	for hash,file in source.iteritems():
		if target.has_key(hash):
			print “%s checksum %s exists in both dirs.” % (file, hash)
			#remove_target_file(file)
			has_dupes=True
	if has_dupes==False:
		print “No duplicate files found.”

def remove_target_file(a):
	os.chdir(target_path)
	os.system(”rm ” + a)
	print “File ” + target_path+”/”+a + ” removed”

def main():
	try:
		gather_files(source_path,source)
		gather_files(target_path,target)
		remove_matches()
	except:
		print “error”

main()

Python

Python: Dictionary examples.

December 2nd, 2008
#!/usr/bin/python
#dictionary
source={}

source["123"] = “filename.txt”
source["444"] = “fit”

for i,v in source.iteritems():
print “%s %s” % (i,v)

# python dict.py
123 filename.txt
444 fit

Uncategorized

Using the ‘Find’ command.

November 24th, 2008

To find files (starting in the current directory) with names ending with .data and to print their names, try this:

find . -name ‘*.data’ -print
company.data
donor.data
grades.data
sorted.data
words.data

To find files larger than 40K and print the file names and details (use a minus sign instead of a plus sign to find files smaller than a certain size), issue this command:

find . -size +40k -ls
-rw-rw-r– hermie users 56720 Jan 16 12:42 bigfile
-rw-rw-r– hermie users 415206 Feb 27 21:37 largefile
-rw-rw-r– hermie users 315428 Jan 07 05:23 hugefile

To find files ending with .dat that are smaller than 100K, enter

find . -name *.txt -size -100k -ls
-rw-rw-r– hermie users 26720 Feb 06 23:52 recipes.txt
-rw-rw-r– hermie users 506 Feb 18 18:45 poem.txt

To find files that have not been accessed for over 30 days and delete them (by sending their names to the rm command), enter

find . -atime +30 -exec rm {} \;

To find directories (starting in the junk directory) and conditionally delete them (by sending their names to the rmdir command), enter

find junk -type d -ok rmdir {} \;

From: http://lowfatlinux.com/linux-find.html

Uncategorized

Fixing a corrupt mysql database

November 13th, 2008

Do a check on the database and verify if there are errrors:

root@psedev2:/elog# mysqlcheck -u root -p jesterads
jester_jesterads.ajax_example                      OK
jester_jesterads.banners                           OK
jester_jesterads.category1                         OK
jester_jesterads.category2                         OK
jester_jesterads.category3                         OK
jester_jesterads.event_invites                     OK
jester_jesterads.event_member_list                 OK
jester_jesterads.event_reminders                   OK
jester_jesterads.faq                               OK
jester_jesterads.groups                            OK
jester_jesterads.groups_discussions                OK
jester_jesterads.inventory                         OK
jester_jesterads.location_cities                   OK
jester_jesterads.location_map                      OK
jester_jesterads.location_neighborhoods            OK
jester_jesterads.location_states                   OK
jester_jesterads.log
warning  : Table is marked as crashed
warning  : 3 clients are using or haven’t closed the table properly
error    : Size of indexfile is: 8814592        Should be: 8815616
error    : Corrupt
jester_jesterads.mailing_list                      OK
jester_jesterads.members                           OK

# mysql -u jester_root -p jester_jest
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 479671 to server version: 4.1.22-standard-log

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> check table log;
+———————-+——-+———-+———————————————————-+
| Table                | Op    | Msg_type | Msg_text                                                 |
+———————-+——-+———-+———————————————————-+
| jester_jesterads.log | check | warning  | Table is marked as crashed                               |
| jester_jesterads.log | check | warning  | 3 clients are using or haven’t closed the table properly |
| jester_jesterads.log | check | error    | Size of indexfile is: 8814592        Should be: 8815616  |
| jester_jesterads.log | check | error    | Corrupt                                                  |
+———————-+——-+———-+———————————————————-+
4 rows in set (0.00 sec)

mysql> repair table log;
+———————-+——–+———-+———-+
| Table                | Op     | Msg_type | Msg_text |
+———————-+——–+———-+———-+
| jester_jesterads.log | repair | status   | OK       |
+———————-+——–+———-+———-+
1 row in set (14.66 sec)

Linux, mysql

Resetting a lost mysql root password.

November 11th, 2008

root@donkey:~# /usr/sbin/mysqld --skip-grant-tables

In second term window do this..

root@donkey:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


mysql> update mysql.user set Password=PASSWORD('somepass') where User='root';
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

Quit mysql and log in…

root@donkey:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

Linux

Annoying PC Beep in Linux

November 6th, 2008

Problem: Every time you mistype something or  the cmd prompt doesn;t understand your input I get a annoy system beep that annoys me and my co-workers. I don’t want to disable sound all together as I would like to still listen to music.
Solution: Blacklist that sucker!

In /etc/modprobe.d/blacklist add the following

“blacklist pcspkr”

Then run ’sudo modprobe pcspkr’

*If that doesn’t work you can remove it permanently by issueing: ’sudo modprobe -r pcspkr’

Now no more systems beeps!

Ref: http://ubuntu-tutorials.com/2007/07/26/turning-off-the-system-hardware-beep-linux-tutorial/

Uncategorized

Parse error log and send email

November 4th, 2008

This post gives an example of how I wrote a script that parsed an error log looking for ‘Traceback’ errors and emailing them to the admin on a daily basis. The cool thing about this script is that it keeps a byte track of each time it reads the log file, stores that byte count in a temp file then picks up at the byte count the next day so you don’t get duplicate emails from tracebacks on a previous day…

let’s call this file tracebacks.py

#!/usr/bin/python
import os
import sys
import time
import smtplib
import socket #required to get host name
from email.mime.text import MIMEText #need this for subject line in email

def main():
	line=""
	line_num=0
	last_byte_read=""
	byte_num=""
	stored_byte_num=""
	traceback_count=0

	log_file=open("/var/log/loadscript/<logfilename here>","r")
	tmp_file="/tmp/loadscript.tmp"

	###############################
	#check if required file exists
	###############################
	if os.path.exists(tmp_file):
		writefile = open(tmp_file,'r')
		stored_byte_num=writefile.read()
	else:
		writefile = open(tmp_file,'w')
		writefile.write("")
		writefile.close()

	writefile = open(tmp_file,'r')			 

	################################
	# find last byte searched in file
	################################
	if len(stored_byte_num)>0:
		stored_byte_num=int(stored_byte_num)
		log_file.seek(stored_byte_num)

	data = log_file.readlines()
	was_found=False

	#loop through file
	for x in data:
		#print x.strip()
		line_num=line_num + 1
		if x.startswith('Traceback'):
			traceback_count += 1
			line += "\n+++++++++++++++++++++++++++++++++++++++"
			#########################################################"
			line += "\nError on Line: %s\n" % line_num
			line += "\n"+x
			was_found=True
		if was_found:
			if x.find('File')==2:
				line += x
		else:
			was_found = False

	# record last byte read in tmp file
	last_byte_read=log_file.tell()
	last_byte_read=str(last_byte_read)
	handle=open(tmp_file,'w')
	handle.write(last_byte_read)
	handle.close()

	#print last_byte_read

	#send mail summary
	if was_found==False:
		mail_message = "\nDid not find any Tracebacks!\n"
	else:
		mail_message = "SCRIPT SUMMARY\n"
	       	mail_message += "================================================\n"
		mail_message += "Tracebacks found: %s\n" % traceback_count
		mail_message += "Last byte checked: %s\n" % last_byte_read
		mail_message += "Script will start at this number next search!\n"
       		mail_message += "================================================\n"
		mail_message += line

		send_mail(mail_message)

        print "Errors found and sent to pse-admin"
        #print "%s" % mail_message

def send_mail(mail_message):

	smtpserver='smtp.example.com'
	host = socket.gethostname()

	RECIPIENTS = ['username@domain.com']
	SENDER = ‘root@%s.mascorp.com’ % host
	MESSAGE = “”"Subject: [Nagios] Loadscript Errors
From: nagios@%s.mascorp.com

%s
“”" % (host, mail_message)

	session = smtplib.SMTP(smtpserver)
	smtpresult = session.sendmail(SENDER, RECIPIENTS, MESSAGE)

	session.close()

try:
	main()

except:
        health = ‘UNKNOWN’
        result = 3
        print “Error: Check script”
        sys.exit(1)

Python ,

Nagios Monitoring w/Ubuntu

November 4th, 2008

Will be updated soon…

Linux , ,

Setting up cron jobs

November 4th, 2008

Need to setup a script to run at a specified time every day, week or month. Use cron!

While logged into linux you can view your crontab file by typing the following

$crontab -l

# m h  dom mon dow   command
35 15 * * *    python /home/jesterj/fogbugz_sql_backup.py

Notice the time parameters followed by the system command. The format is follows…

#min | hour  | day month | month | day of week
10 12 * * *  <command here>

The above script would execute at 12:10pm every day.

To edit this file issue the command with an -e option

$crontab -e

To run a command with sudo priviledges, simply issue a ’sudo’ before the cron command like so.

$sudo crontab -e

That’s it!

Uncategorized , ,

SSH logins using Keys (not passwords!)

November 4th, 2008

CREATE KEY ON CLIENT

jesterj@jesterj-laptop:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jesterj/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jesterj/.ssh/id_rsa.
Your public key has been saved in /home/jesterj/.ssh/id_rsa.pub.
The key fingerprint is:
b3:82:51:f6:14:71:d1:48:bf:26:65:60:50:3e:a3:44 jesterj@jesterj-laptop
The key’s randomart image is:
+–[ RSA 2048]—-+
|        E=B+     |
|       . =.o.    |
|      o o + +    |
|     o + . = .   |
|    .   S . o    |
|     o   o o     |
|    . . .        |
|       .         |
|                 |
+—————–+

COPY PUB KEY TO SERVER

jesterj@jesterj-laptop:$ scp ~/.ssh/id_rsa.pub jesterj@donkey.dnsdojo.com:/home/jesterj/

ON REMOTE SERVER, ADD PUB KEY TO AUTHORIZED KEYS

root@donkey:~# cat id_rsa.pub >> .ssh/authorized_keys
root@donkey:~# tail .ssh/authorized_keys
….
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsW9ypBuq/Xj1c6HNp8m45H8Kc3ZkZQanP3QLQWGDFLpdtnUCWfAe4dzQBL+ZaMjX7WRX/9i/YUCxB+589tRRXBLqoQ4OxaqUugdgfa2iBSDo9wMuGqfWhTcVKu0gTpKnocdQQKB2W7KjvEpJYjz4LFMwWvRnpAcy0Pmmd9KP9X9LDfRyLCjn8qYBzZ69eoJgQc8zA7gD6Cj0nGiQNlFo3yKvonyZEFO/hXja3SIM4XttYG+CLHCR2q1Itstdw9K8ZjhCXyxUl0K78AysT1nzZ5DRLoPb4GZ27oHcGQuS2127UEiMYUszDZRluXTZik679BZQlhZyGQdtEZMw/jt11Q== jesterj@jesterj-laptop

TEST FROM CLIENT

jesterj@jesterj-laptop:~/.ssh$ ssh jesterj@donkey.dnsdojo.com
Linux donkey.dnsdojo.com 2.6.28-17-generic #58-Ubuntu SMP Tue Dec 1 18:57:07 UTC 2009 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/

Last login: Tue Jan 12 14:31:21 2010 from 206-169-197-253.static.twtelecom.net
jesterj@donkey:~$

SUCCESS!

Linux ,

Synergy

November 4th, 2008

Using Synergy with Vista & Ubuntu

-install synergy on both boxes.
-pick one box to be the server and one the client.

-ubuntu will be the client here. use actual computer names. save as synergy.conf

////
section: screens
        titan.zipcon.net:
        jeremiah-PC:
end

section: links
        titan.zipcon.net:
                left = jeremiah-PC

        jeremiah-PC:
                right  = titan.zipcon.net

end
///

-then on ubuntu run the server
#sudo screen synergys -f --config /home/jester/synergy.conf

-add to rc.local to start on startup.

-the on client (windows box) type in computer ip and click 'start'. enable auto start on login.

Linux

Shutdown if HDD temp too high. (Script)

November 4th, 2008

Upcoming topics…

October 31st, 2008

-sample python scripts

-nagios configure and install

-saving ssh keys.

-virtual box setup (bridging network)

Uncategorized

grep the file system

October 31st, 2008

Find all files matching a particular phrase on your file system.

#the minus R option searches recurisively through files.

~$ sudo grep -R “shutdown” /var/log/messages

Oct 28 09:25:00 jeremiah-laptop nagios2: Successfully shutdown… (PID=6230) Oct 29 16:03:33 jeremiah-laptop nagios2: Successfully shutdown… (PID=6206)

Uncategorized