Listing the sizes of all DB’s in mysql

#
###########################################################
#I can never find this online so I'm adding it to my site.#
###########################################################
#
SELECT table_schema "Database Name", sum( data_length + index_length ) / 1024 / 1024 "Database Size in MB" FROM information_schema.TABLES GROUP BY table_schema;

DSquery and get channing

## This is a quick and dirty way of grabbing membership data out of AD
## first query the group name you are looking for, wild cards can be used as will (*)
## then get the members of the group
## then get the users first name, last name, and email address and dump it in to a file.
dsquery group -name "<group_name>" | dsget group -members |dsget user -fn -ln -email >> C:\file.txt

simple yet powerful AD ldap foo

# Script is designed to dump the email addresses of all cluster users
# This will ensure that AD is synced with the Hptc-users-list.lists.fas.harvard.edu
# mailman list.
# Written by: Luis Silva 04/13/2012
#
import os,ldap
def get_ldap_connection(): #funtion for binding to AD via LDAP
    """
    Bind to server, return connection
    """
    #set up ldap connection
    server='' # server IP goes here
    who='' # dn of a service account goes here
    cred=''# The password for that account here
    print 'pre-open'
    l=ldap.open(server)
    print 'post-open'
    l.protocol_version = 3
    l.simple_bind_s(who,cred)
    print "Successfully Bound to server.\n"
    return l
def get_email_address(username): # function that searches AD for a users and returns thier principal email address
	global LDAP_CONN
	ldap_conn = LDAP_CONN
	base ='ou=Domain Users,dc=rc,dc=domain'
	cgrbase='ou=CGR,dc=rc,dc=domain'
	scope = ldap.SCOPE_SUBTREE
	ldap_filter = '(&(objectClass=person)(sAMAccountName=%s))' %(username)
	attrs = ['mail']
	scrhArray = []
	rcsrch = ldap_conn.search_ext_s(base,scope,ldap_filter,attrs)
	cgrsrch = ldap_conn.search_ext_s(cgrbase,scope,ldap_filter,attrs)
	srchArray = [rcsrch,cgrsrch]
	srch_results_mail = 'null'
	for srch in srchArray:
		for dn,keys in srch:
			if keys.has_key('mail'):
				srch_results_mail = keys['mail'][0]
				print srch_results_mail
	return srch_results_mail
def get_status(username): # function that searches AD for the user and returns thier User Account Control attribute
	global LDAP_CONN
	ldap_conn = LDAP_CONN
	base ='ou=Domain Users,dc=rc,dc=domain'
	cgrbase='ou=CGR,dc=rc,dc=domain'
	scope = ldap.SCOPE_SUBTREE
	ldap_filter = '(&(objectClass=person)(sAMAccountName=%s))' %(username)
	attrs = ['userAccountControl']
	scrhArray = []
	rcsrch = ldap_conn.search_ext_s(base,scope,ldap_filter,attrs)
	cgrsrch = ldap_conn.search_ext_s(cgrbase,scope,ldap_filter,attrs)
	srchArray = [rcsrch,cgrsrch]
	srch_results_status = 'null'
	for srch in srchArray:
		for dn,keys in srch:
			if keys.has_key('userAccountControl'):
				srch_results_status = keys['userAccountControl'][0]
				print srch_results_status
	return srch_results_status

if __name__=='__main__':
	LDAP_CONN = get_ldap_connection() # binds to LDAP
	os.system("getent passwd |grep -e '/n/home[00-13]' -e '/users'>datafile.txt") #dumps grep search to file for parsing later
	reader=open("datafile.txt") # opens data file for reading
	f = open('emaildump.txt', 'w') # opens file where all the valid email address will go later
	nadafile = open('noemailaddy.txt', 'w') # Opens file where users with no email attributes fullname will be stored
	for lines in reader.readlines(): # loop through the file with the ldap query data in it
		#print lines
		linearray=[] # throwing everthing in an array
		linearray.append(lines.split(":")) # splitting on the colon becuase that how getent outputs stuff
		for username,w,uid,gid,fullname,homedir,shell in linearray: # looping through while assigning a variable to all the attributes
			print username
			uac=get_status(username)
			if uac == '512'or uac == '66048':
				if get_email_address(username) == 'null':
					print "This user has no email address: %s" %fullname
					nadafile.write("%s\n" %fullname)
				else:
					print "this user has an active account... writing out to emaildump.txt"
					f.write("%s\n" %get_email_address(username))
			elif uac == '514':
				print "### This is a disabled account ###"
	f.close()
	nadafile.close()

mailman sync with AD script

# This is for sycing email addresses from all the "G_" groups
# in the RC domain to matching "G_" groups in mailman.
#
# Author Luis Silva 06-13-2011

import os,sys,string,ldap
from datetime import datetime
import time

def msg(m): print m
def dashes(): print '-' * 40
def msgt2(m): msg('>> %s' % m)
def msgt(m): dashes(); msg(m); dashes()
def msgx(m): msgt(m); sys.exit(0)

# Deifnes who is the list manager and the defaultPw for all the lists.
adminEmail = ''
defaultPw = ''
mailmanCmdLocation = '/usr/lib/mailman/bin/'
pwd = '/mailmanSync/groupSync/temp_files'

def get_ldap_connection():
    """
    Bind to server, return connection
    """
    #set up ldap connection
    server=''
    who='cn=groupsync,ou=Unmanaged Service Accounts,dc=rc,dc=domain'
    cred=''
    print 'pre-open'
    l=ldap.open(server)
    print 'post-open'
    l.protocol_version = 3
    l.simple_bind_s(who,cred)
    print "Successfully Bound to server.\n"
    #print l
    return l

LDAP_CONN = get_ldap_connection()  

def get_ldap_group_obj( groupname):
    global LDAP_CONN
    """ Searches, bases on the cn"""
    base ='ou=Lab_Instruments,ou=Domain Groups,ou=CGR,dc=rc,dc=domain'
    scope = ldap.SCOPE_SUBTREE
    ldap_filter = 'sAMAccountName=%s' % groupname
    attrs = ['*']
   # print ldap_filter
    srch_results = LDAP_CONN.search_ext_s(base,scope,ldap_filter,attrs)
    return srch_results

LDAP_QUERY_CNT = 0
EMAIL_LOOKUP_CNT =0
"""
Need to fix this for efficiency, not to run hundreds of queries
"""
USER_EMAIL_LOOKUP = {}	# { userDN : email }
def get_ldap_user_obj(userDN):
    global USER_EMAIL_LOOKUP, EMAIL_LOOKUP_CNT
    EMAIL_LOOKUP_CNT +=1
    if USER_EMAIL_LOOKUP.has_key(userDN):
         return USER_EMAIL_LOOKUP.get(userDN, None)

    """ Searches, bases on the dn"""
    global LDAP_CONN, LDAP_QUERY_CNT
    ldap_conn = LDAP_CONN
    base ='ou=Domain Users,dc=rc,dc=domain'
    cgrbase='ou=CGR,dc=rc,dc=domain'
	#Old OU's that have been moved into Domain Users
    #mcbbase='ou=MCB,dc=rc,dc=domain'
    #oebbase='ou=OEB,dc=rc,dc=domain'
    #ccbbase='ou=ccb,dc=rc,dc=domain'
    #cnsbase='ou=CNS,dc=rc,dc=domain'
    scope = ldap.SCOPE_SUBTREE
    #ldap_filter = '(&(objectCategory=person)(objectClass=User))'
    ldap_filter = '(&(objectClass=person)(distinguishedName=%s))' % userDN
    attrs = ['distinguishedName', 'mail']
    scrhArray = []

    seersrch = ldap_conn.search_ext_s(base,scope,ldap_filter,attrs)
    cgrsrch = ldap_conn.search_ext_s(cgrbase,scope,ldap_filter,attrs)
    #mcbsrch = ldap_conn.search_ext_s(mcbbase,scope,ldap_filter,attrs)
    #oebsrch = ldap_conn.search_ext_s(oebbase,scope,ldap_filter,attrs)
    #ccbsrch = ldap_conn.search_ext_s(ccbbase,scope,ldap_filter,attrs)
    #cnssrch = ldap_conn.search_ext_s(cnsbase,scope,ldap_filter,attrs)
    srchArray = [seersrch,cgrsrch]
    srch_results = 'null'
    for srch in srchArray:
    	#print '>> using srch', srch
	for dn, keys in srch:
		if (dn == userDN):
			if keys.has_key('mail'):
				srch_results = keys['mail']
			        LDAP_QUERY_CNT+=1
    				USER_EMAIL_LOOKUP.update({userDN:srch_results})
	   			#msg('(%s) Found! %s' % (LDAP_QUERY_CNT, keys['mail']))
			else:
    				USER_EMAIL_LOOKUP.update({userDN:None})
    return srch_results

def start_mailman_sync():

	global noEmailDNList
	noEmailDNList = []
	groupname = '*' #_test'
	groupObj = get_ldap_group_obj(groupname)

	rcGroupArray =[]
	for a,b in groupObj:
		rcGroupArray.append(b['sAMAccountName'])
		print b["sAMAccountName"]

	#print rcGroupArray
	msg('# of AD groups to check: %s' %  len(rcGroupArray))

	mailmanLists = os.popen(mailmanCmdLocation +'/list_lists -b').readlines()

	#print mailmanLists
	msg('# of mailman groups to check: %s' %  len(mailmanLists))
	reconcile_mailmain_lists(mailmanLists, rcGroupArray)

def get_member_emails_from_group(groupname):
	msgt2('get_member_emails_from_group: %s' % groupname)
	if groupname is None:
		return []

        groupObj =get_ldap_group_obj( groupname)
        rcMemberArray =[]
        for c,d in groupObj:
		if d.has_key('member'):
			for mem in d['member']:
				#print mem
				userDN = mem
				userMail = get_ldap_user_obj(userDN)
                                #get_ldap_user_obj('CN=Kunal Tiwari,OU=SEER,OU=Domain Users,DC=rc,DC=domain')
				#print userMail[0]
				if userMail is not None:
					rcMemberArray.append(userMail[0])
				else:
					noEmailDNList.append(userDN)

	rcMemberArray = filter(lambda x: x is not None and len(str(x)) >= 3, rcMemberArray)
	rcMemberArray.sort()
	return rcMemberArray

def get_mailman_list_members(list_name):
        msgt2('get_mailman_list_members: %s' % list_name)

     	if list_name is None:
		return []
	str_get_members_cmd = '%slist_members %s' % (mailmanCmdLocation, list_name)
	mailman_members = os.popen(str_get_members_cmd).readlines()
        mailman_members = map(lambda x: x.strip(), mailman_members)
        return mailman_members

def pause():
	nsec =0 #1
	msg('%s second pause' % nsec)
	time.sleep(nsec)

def read_temp_file(fname):
	msg('open file: %s' % fname)
	fh = open(fname, 'r')
	flines = fh.readlines()
	fh.close()
	return flines

def write_temp_file(fcontent, fname):
	msg('write file: %s' % fname)
	fh = open(fname, 'w')
	fh.write(fcontent)
	fh.close()
	pause()

def get_temp_filename(file_prefix='temp'):
	now = datetime.now()
	return os.path.join(pwd, '%s_%s%s.txt' % (file_prefix, now.strftime('%m-%d-%Y_%I-%M-%S'), now.microsecond))

def remove_extra_members_from_mailman(groupname, mailman_members_to_remove):
        msgt2('remove_extra_members_from_mailman: [%s][%s]' % (groupname, mailman_members_to_remove))

        if groupname is None or mailman_members_to_remove is None or len(mailman_members_to_remove) == 0:
                return
	fname = get_temp_filename(groupname)
	write_temp_file('\n'.join(mailman_members_to_remove), fname)
        msg('file written')

	str_remove_members = '%sremove_members -f %s %s' % (mailmanCmdLocation,fname,groupname)
        msg('mailman cmd: [%s]' % str_remove_members)
        os.system(str_remove_members)
        #os.popen(str_remove_members)
        msg('mailman command executed')

def add_members_to_mailman_list(groupname, new_mailman_members):
	msgt2('add_members_to_mailman_list: [%s][%s]' % (groupname, new_mailman_members))

	if groupname is None or new_mailman_members is None or len(new_mailman_members) == 0:
   		return
        fname = get_temp_filename(groupname)
	write_temp_file('\n'.join(new_mailman_members), fname)
	msg('file written')

	str_add_members = '%sadd_members -r %s %s' % (mailmanCmdLocation,fname,groupname)
	msg('mailman cmd: [%s]' % str_add_members)
	os.system(str_add_members)
	#os.popen(str_add_members)
	msg('mailman command executed')

def make_new_mailman_list_and_populate(groupname):
 	msgt2('make_new_mailman_list_and_populate: %s' % groupname)
	strNewlist = '%snewlist -q %s %s %s' %(mailmanCmdLocation,groupname,adminEmail,defaultPw)
	print strNewlist
	os.system(strNewlist)
	#os.popen(strNewlist)
	msgt2('New list created: %s' % groupname)

	rcMemberArray = get_member_emails_from_group(groupname)
	print rcMemberArray
	print len(rcMemberArray)
        print '\n'.join(rcMemberArray)

        fname = get_temp_filename(groupname)
        write_temp_file('\n'.join(rcMemberArray), fname)
	strAddMem = '%sadd_members -r %s %s' %(mailmanCmdLocation, fname,groupname)
        print strAddMem
        os.system(strAddMem)
	#os.popen(strAddMem)
	#subprocess.call(strAddMem.split())
def lower_and_strip_list(lst):
	lst = map(lambda x: x.lower().strip(), lst)
        return filter(lambda x: len(x) > 0, lst)

def reconcile_mailmain_lists(mailmanLists, rcGroupArray):
	msgt('reconcile_mailmain_lists')
	if mailmanLists is None or rcGroupArray is None:
		print 'reconcile_mailmain_lists: No lists to reconcile'
		return
	loop_counter =0
	really_sync = False
	for rcItem in rcGroupArray:
		loop_counter +=1
	  	ad_group_name= rcItem[0].strip()
                msg('')
		msgt('(%s) check list: %s' % (loop_counter, ad_group_name))
		print 'lookup size: %s' % len(USER_EMAIL_LOOKUP)
		print 'email lookups: %s' % EMAIL_LOOKUP_CNT
		itemExists = False
		for mailmanItem in mailmanLists:
			rc = ad_group_name.lower()
			man = mailmanItem.lower().strip()

			if (rc == man):
				print 'AD group matches Mailman group name'
				itemExists = True
				break
		#print itemExists
		if loop_counter >= 0:	#ad_group_name == 'G_AWM_Inverted':
			really_sync=True
                else:
                	really_sync=False
		if really_sync:
                        if ad_group_name.find(' ') > -1:
    				msgx('spaces found in AD name: [%s]' % ad_group_name)

			elif not itemExists:
				#msgt('make_new_mailman_list_and_populate: %s' % rcItem[0])
				make_new_mailman_list_and_populate(ad_group_name)

			elif itemExists:
				msgt('reconcile lists: %s' % ad_group_name)

				# retrieve/format AD group email addresses
				rcMemberArray = get_member_emails_from_group(ad_group_name)
				rcMemberArray = lower_and_strip_list(rcMemberArray)

				# retrieve/format mailman email addresses
				list_members = get_mailman_list_members(ad_group_name)
				list_members = lower_and_strip_list(list_members)

				# add missing members to mailman
				new_mailman_members = []
				for ad_email in rcMemberArray:
					if not ad_email in list_members:
						new_mailman_members.append(ad_email)
				if len(new_mailman_members) > 0:
					add_members_to_mailman_list(ad_group_name, new_mailman_members)

				# remove extra members from mailman
				mailman_members_to_remove = []
				for mm_email in list_members:
					if not mm_email in rcMemberArray:
						mailman_members_to_remove.append(mm_email)
				if len(mailman_members_to_remove) > 0:
					remove_extra_members_from_mailman(ad_group_name, mailman_members_to_remove)

				if len(mailman_members_to_remove) == 0 and len(new_mailman_members)==0:
					msg('groups are the same')
				else:
					msg('> groups reconciled')
					dashes()
					print 'rcMemberArray',get_member_emails_from_group(ad_group_name)
					dashes()
					print 'list_members', get_mailman_list_members(ad_group_name)
                        #if ad_group_name == 'G_ABI7900': msgx('exit')

def remove_temp_files():
	msgt('remove temp files')
	global pwd
	fcnt =0
	for fname in os.listdir(pwd):
		fullpath = os.path.join(pwd, fname)
		if os.path.isfile(fullpath) and fname.endswith('.txt'):
			os.remove(fullpath)
			print 'temp file removed: %s' % fullpath
			fcnt+=1
	msg('# files deleted: %s' % fcnt)

def delete_test_mailman_list():
	os.popen('/usr/lib/mailman/bin/rmlist G_ABI7900')
	print 'list deleted'

#delete_test_mailman_list()

def write_noEmail_file():
	f = open('/mailmanSync/groupSync/noEmailList.txt','w')
	output = []
	for DNs in noEmailDNList:
		if DNs not in output:
			output.append(DNs)
	for uniqueDNs in output:
		f.write('%s\n' % uniqueDNs)

	f.close()

if __name__=='__main__':
	#delete_test_mailman_list()
	start_mailman_sync()
	remove_temp_files()
	write_noEmail_file()

mysql backup script

# Written by: Luis Slva
# Date: 08-21-2011
#
# The srcipt is designed to be run in cron in order to backup the databases on any given mysql machine.
# It will first list the databases then do a mysqldump on each database individually and compress the output to a file share.
#
import os
import os.path
from datetime import datetime, timedelta, date
#
# All perameters are set by editing these values:
#
mysqlServer = "" 				#FQDN of your mysql server (localhost works too)
mysqlUser = "" 					# username that has access usually root
accessGroup = "" 				# group that you would like the zip files owned by
pwFile = "" 					# make a file with the password as the first line in it and lock it down
mysqlPwd = open(pwFile, 'r').read().strip() 	# Read that file in here
backupDir = "" 					# the directory that all the backups will go in
#
# Code starts here!:
#
# setting some variables here
now = str(datetime.now()) # These are of type datetime
wkAgo = datetime.now() - timedelta(days=7) # type datatime again
strWkAgo = str(datetime.now() - timedelta(days=7)) # I need to convert the week ago date to a string so I can match on it later
nowFormatted = now.split(" ")[0] # getting just the date splitting off the time
wkAgoFormatted = strWkAgo.split(" ")[0] # getting just the date splitting off the time
#
# extracts the date from the file name
def get_file_date(fileName):
	strFileDate = fileName.split('.')[1]
	return datetime.strptime(strFileDate,"%Y-%m-%d")
#
# executes what ever mysql command you give it.
def execute_mysqlCommand(cmd):
	return os.popen(('mysql -h %s -u %s -p%s -e "%s;"') %(mysqlServer,mysqlUser,mysqlPwd,cmd))
#
# runs a mysqldump on what ever database name you give it.
def execute_mysqlDump(dbName):
	dumpString = (('mysqldump -h %s -u %s -p%s %s &gt; %sDATABASE-%s.%s.sql') %(mysqlServer,mysqlUser,mysqlPwd,dbName,backupDir,dbName,nowFormatted))
	print dumpString
	return os.popen(dumpString)
def zipAll():
	os.popen('tar -czvf  %sDATABASE-ALL.%s.sql.tgz %s%s' %(backupDir,nowFormatted,backupDir,zipString))
	return
#
#
dblist = execute_mysqlCommand("show databases")

print execute_mysqlCommand("FLUSH TABLES WITH READ LOCK")
first = "True"
for dbName in dblist: # Loops through all the databases names
	if first == True:
		first = "Flase"
	elif dbName.strip() == "Database":
		print "skipping title"
	else:
		dbName = dbName.strip()
		print "atempting to create: " + dbName
		execute_mysqlDump(dbName)
		print dbName + " successfully completed"

print execute_mysqlCommand("UNLOCK TABLES")

# read backup directory and delete files older that 7 days
def get_attribs():
	bkpFiles = os.popen(('ls -lh %s') %(backupDir))
	attribs= []
	first = True
	for files in bkpFiles:
		if first:
			print "skipping first line:"
			first = False
		else:
			print files.split()
			attribs.append(files.split()[8])
	return attribs

bkpDirList = get_attribs()
print bkpDirList
for fileName in bkpDirList:
	FileDate = get_file_date(fileName)
	if FileDate &gt; wkAgo:
		print "kept: " + fileName
	else:
		os.popen(('rm -fv %s%s') %(backupDir, fileName))
		print "deleted: " + fileName
#
# building zip of all databases
zipString = "*.sql"
zipAll()
# delete all the sql files left behind
os.popen(('rm -fv %s*.sql') %(backupDir))

#set permissions so that database admins can use these for data recovery
os.popen(('chown -R root:%s %s') %(accessGroup,backupDir))
os.popen(('chmod -R 770 %s') %(backupDir))</pre>

auto make home directories

# Written by: Luis Silva
# Date 10-04-2011
#
# 1. Makes homedirectory for a new user in /n/ncfusers/&lt;username&gt;
# 2. Copies defualt configuration data into there new home directory /n/ncfusers/default-profile/ =&gt; /n/ncfusers/&lt;username&gt;
#
from subprocess import Popen, PIPE
import sys
# constants
defaultConfPath = "/n/ncfusers/default-profile/"
homeDirPath = "/n/ncfusers/"

# Test to make user that thier are 2 arguments ahead of the command.
def testArgs():
	count = 0
	for arg in sys.argv:
		count += 1
		if count == 2:
			username = arg
		elif count == 3:
			groupname = arg
	if count &lt; 3:
		sys.exit("not enough arguments!\nPlease user the propper syntax:\npython mk-ncfhomedir.py &lt;username&gt; &lt;group&gt;")
	elif count &gt; 3:
		sys.exit("too many arguments!\nPlease user the propper syntax:\npython mk-ncfhomedir.py &lt;username&gt; &lt;group&gt;")
	return (username, groupname)

# Run Test and get variables
username, groupname = testArgs()

# Make new home directory
dirPath = homeDirPath + username

makedir = Popen(['mkdir', dirPath], stdout=PIPE, stderr=PIPE)
mkOutput = makedir.stdout.read()
mkError = makedir.stderr.read()

if mkOutput == "" and mkError == "" :
	print "Home directory created: " + dirPath
else:
	print "Output: " + mkOutput
	print "Error: "+ mkError

# copy data from default directory. 

cpData = Popen(['rsync','-av', defaultConfPath, dirPath], stdout = PIPE, stderr=PIPE)
cpOutput = cpData.stdout.read()
cpError = cpData.stderr.read()

if cpOutput == "":
	print "Error: "+ cpError
else:
        print "Output: " + cpOutput

# Changing ownership in the home directory to be owned by the user.
chownData = Popen(['chown','-Rv', username + ":" + groupname, dirPath ], stdout = PIPE, stderr=PIPE)
chOutput = chownData.stdout.read()
chError = chownData.stderr.read()

if chOutput == "" :
	print "Error: "+ chError
else:
        print "Output: " + chOutput
print "done!"

xfs_quota dump to xls

# Script to pull out quota data in to a spreadsheet
# Writen by Luis Silva 03-14-2012
#
import os
import xlwt
#
wb = xlwt.Workbook() #open workbook
ws = wb.add_sheet('raw_data') # add sheet
a=[] # start an empty array
rptout = os.popen('xfs_quota -x -c "report -pN"') # run os command and collect out put to rptout
for lines in rptout.readlines(): # loop through output strip out new line character and break it up by spaces
	lines = lines.strip("\n")
	cdata = lines.split(" ")
	for column in cdata: # get rid of empty spaces and put it all into and array
		if len(column) &gt; 0 :
			a.append(column)
col=0
row = 1
headings = ["Project ID","Actual Use", "Soft Quota","Hard Quota","Warn/Grace"]
x=0
for things in headings: # Write out headings
	ws.write(0,x, things)
	x=x+1
for items in a: # Loop through the array of items and write 5 times accross then go to the next row.
	if col==5:
		col=0
		row = row + 1
	else:
		ws.write(row,col,items)
		print items
		col=col+1
wb.save('nssdeep.xls')