Skip to content

Instantly share code, notes, and snippets.

@andreisavu
andreisavu / threadpool.py
Created June 4, 2009 12:30
Python Thread Pool
# Original code at http://code.activestate.com/recipes/203871/
# I have added a limit for the number of tasks waiting
# The method queueTask will block if the current taskList size
# exceeds the specified limit
import threading
from time import sleep
# Ensure booleans exist (not needed for Python 2.2.1 or higher)
@andreisavu
andreisavu / ssh-copy-id
Created June 4, 2009 13:39
ssh-copy-id with port parameter. Usage: ssh-copy-id -p 1022 user@server
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
ID_FILE="${HOME}/.ssh/id_rsa.pub"
if [ "-i" = "$1" ]; then
@andreisavu
andreisavu / check_rps.py
Created June 12, 2009 08:36
Nagios plugin for measuring the peak and average number of requests a server receives.
#! /usr/bin/python
"""
Measure the number of request per seconds the
server has received in the last n requests (peak and average)
You can use it to analyze log files from apache and nginx
or any other server that uses the same log format as apache.
This script is designed to be used as a nagios plug-in.
"""
# tested with hbase 0.19.3 and jython 2.2.1
import java.lang
from org.apache.hadoop.hbase import HBaseConfiguration, HTableDescriptor, HColumnDescriptor, HConstants
from org.apache.hadoop.hbase.client import HBaseAdmin, HTable
from org.apache.hadoop.hbase.io import BatchUpdate, Cell, RowResult
# First get a conf object. This will read in the configuration
# that is out in your hbase-*.xml files such as location of the
# hbase master node.
# smart backup using rsync : rsync_snapshot
# http://www.mikerubel.org/computers/rsync_snapshots/
mv backup.3 backup.tmp
mv backup.2 backup.3
mv backup.1 backup.2
mv backup.0 backup.1
mv backup.tmp backup.0
cp -al backup.1/. backup.0
rsync -a --delete source_directory/ backup.0/
#----------------------------------------------------------------
#
# my.cnf file
#
#
# See:
#
# http://dev.mysql.com/doc/refman/5.1/en/server-options.html
# http://dev.mysql.com/doc/refman/5.1/en/option-files.html
# Returns a bytestring version of 's', encoded as specified in 'encoding'.
def smart_str(s, encoding='utf-8', errors='strict'):
"""
Returns a bytestring version of 's', encoded as specified in 'encoding'.
"""
if not isinstance(s, basestring):
try:
return str(s)
except UnicodeEncodeError:
@andreisavu
andreisavu / http_serve.py
Created November 29, 2010 11:19
Dummy HTTP server useful for testing
#!/usr/bin/env python
""" Test HTTP Server
This script starts a http server that will respond to HTTP requests
with a predefined response.
Usage:
./http_server.py --port=8080 --code=404 --content="Page not Found"
@andreisavu
andreisavu / prime_factors.scala
Created March 8, 2011 22:16
Find prime factors for an integer
import scala.math.{ceil,sqrt}
object Main {
def isPrime(n: Int): Boolean =
if (n <= 2) (n == 2) else {
val limit = ceil(sqrt(n)).toInt
def _check(i: Int): Boolean =
if (i > limit) true
import logging
import riak
log = logging.getLogger(__name__)
class RiakCounter(object):
def __init__(self, bucket, key):
self.bucket = bucket
self.bucket.set_allow_multiples(True)
self.key = key