Comments

Making the Bird Tweet Using Python

After taking another look at Python I am quickly coming to love it, as an “exercise” in re-learning python I decided to write a very simple command line “tweeter” this uses the Twitter API to update your twitter status, extending from the ”update twitter in a single line

You can grab a copy of the script from here: https://svn.blog.oneiroi.co.uk/branches/python/tweet.py

UPDATE 24/03/2011:Oauth version here

Example usage:

./tweet.py -u username -p password -t your tweet goes here

If you want to parse the JSON data normally returned after submitting a new tweet simply add the -j flag.

If you are prompted for a username and password when running this script the username and password supplied using the -u and -p flags was incorrect.

php
Comments

Wordpress Getting the Current Page or Post ID

An example of getting the current page / post ID, identifying whether the current item is a page or a post, and then appending the results to the content.

All from within a plugin.

<?PHP
/*
Plugin Name: Get Page / Post ID using a plugin by D.Busby Saiweb.co.uk
Plugin URI: https://blog.oneiroi.co.uk
Description: Identifies the current page/post and appends text to the content
Version: 0.1
Author: David Busby
Author URI: https://blog.oneiroi.co.uk
*/

//WP hooks start
add_filter('the_content', 'post_page');
//WP hooks end

function post_page($content){
    global $post; //wordpress post global object
    if($post->post_type == 'page'){
        $content .= '<br /> This item is a page and the ID is: '.$post->ID;
    } elseif($post->post_type == 'post') {
        $content .= '<br /> This item is a post and the ID is: '.$post->ID;
    }
    return $content;
}
?>

Install the above as a plugin i.e. in wp-content/plugins/test/test.php

Head over to your admin menu and enable the plugin, now each page and post will identify itself as a page or post, and provide it’s ID.

There is a lot available in the $post object for a list add

ob_start();
var_dump($post);
$content .= ob_get_contents();
ob_end_clean();
Comments

mySQL Bash Backup Script

In on of those “oh ffs” moments I found myself writing a BASH script to quickly dump all database on a mySQL server.

#!/bin/bash
MYSQL=`which mysql`;
MYSQLDUMP=`which mysqldump`;
GZIP=`which gzip`;
DEST="/path/to/dump/folder"

USER="root";
PWD="XXXXXX";

DBS=(`$MYSQL  -u $USER -p$PWD  -Bse 'show databases'`);

for db in ${DBS[@]};
do
        `$MYSQLDUMP --default-character-set=utf8 --set-charset -u $USER -p$PWD $db | $GZIP -9 > $DEST/$db.sql.gz`
        echo "$db - DONE";
done;

This script gets a list of all databases, dumps them out with UTF8 encoding, and gzip compresses the SQL file into the given “DEST” folder.

If you want to skip over certain databases i.e. “mysql”

Change this line:

DBS=(`$MYSQL  -u $USER -p$PWD  -Bse 'show databases'`);

To:

DBS=(`$MYSQL  -u $USER -p$PWD  -Bse 'show databases' | grep -v "database_to_exclude"`);

Or for multiple exclusions

DBS=(`$MYSQL  -u $USER -p$PWD  -Bse 'show databases' | grep -v "database_to_exclude" | grep -v "another_database_to_exclude" | grep -v "etc"`);

I may re-write this in Python, if I get time.

Comments

Update Twitter in a Single Line

As it turns out twitter account can be updated in a single line, this makes writing “bots” just that little bit easier.

/usr/bin/curl --basic --user "username:password" --data-ascii "your tweet" https://twitter.com/statuses/update.json

This also returns JSON should you want to parse the reply data.

i.e.

{"in_reply_to_screen_name":null,"in_reply_to_status_id":null,"truncated":false,"user":{"profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","description":"","followers_count":0,"screen_name":"user","url":null,"name":"user","protected":true,"location":"","id":12345678},"text":"your tweet","favorited":false,"created_at":"Fri Mar 20 11:38:44 +0000 2009","in_reply_to_user_id":null,"id":1359757870,"source":"web"}

At the moment I am looking at hooking this into Nagios, from there the feed will be passed into a ‘service status page’.

But in theory this single line could be used for any purpose.

Comments

mySQL Slow Query Log Rotation

One of the issues facing log rotation in mySQL is that mySQL doesn’t seem to have the ability to perform a “reload”.

Meaning standard methods of rotating logs using logrotate leave mySQL logging an error in the syslog saying that the log file could not be found, and refusing to to any logging until the server is restarted, not fun if like me you manage high availability solutions and restarting a service is never the best option.

As such I have written this log rotate script, this does however make some assumptions.

  1. Your mysql user is “mysql”
  2. Your mysql slow query log is /var/log/mysql-slow.log

The script is written to perform the following actions:

  1. Rotate weekly
  2. Retain 3 rotations (3 files + live log)
  3. Compress on rotate (gzip)
  4. Create new logfile with 660 permissions chowned to mysql:mysql
  5. Run: mysqladmin flush-logs

Please be aware that the flush logs command will also rotate any binary logging currently in place, please ensure this will not adversely affect your deployment prior to use

Please ensure you carry out your own testing prior to deploying this script into a live environment.

https://svn.blog.oneiroi.co.uk/branches/linux-the-sysadmin-script/branches/logrotate.d/mysql

#
# mySQL slow log rotation script by D.Busby
# place this script in /etc/logrotate.d/ or your appropriate logrotate dir.
# https://creativecommons.org/licenses/by-sa/2.0/uk/ CC BY-SA
#
# NOTE: if you are reliant on binlogs i.e. for replication, 'flush logs' closes the current binlog and moves to the next itteration of log files
# You should test this does not cause an issue with your deployment before using this script
# 
/var/log/mysql-slow.log {
    weekly
    rotate 3
    compress
    missingok
    notifempty
    sharedscripts
    create 660 mysql mysql
    postrotate
        /usr/bin/mysqladmin flush-logs
    endscript
}