Skip to content

Instantly share code, notes, and snippets.

View siroken3's full-sized avatar

Kenichi Sasaki siroken3

View GitHub Profile
@siroken3
siroken3 / resolv.py
Created June 22, 2012 17:12
botoを使って自身のinstanceのタグを得る
#! /user/bin/env python
from subprocess import check_cal
import urllib2
import boto.ec2
f = urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id')
id = f.readline()
region = boto.ec2.get_region('us-west-2')
@siroken3
siroken3 / gist:3422452
Created August 22, 2012 05:20
pythonから sqlite3を使うサンプル (カラム名使う編)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
if __name__ == "__main__":
con = sqlite3.connect("dbname.db")
con.row_factory = sqlite3.Row
for row in con.execute("select col1, col2 from table_name"):
print row['col1'], row['col2']
@siroken3
siroken3 / gist:3445985
Created August 24, 2012 05:39
指定した名前のRegisonInfoを得る
#!/bin/env python
import boto.ec2
region_info = [r for r in boto.ec2.regisons() if r.nmae == '<REGION-NAME>'][0]
@siroken3
siroken3 / gist:3446579
Created August 24, 2012 06:25
なんか怒られる例 (BotoServerError: 400 Bad Request)
#!/bin/env python
import boto
import boto.ec2
regison = boto.ec2.get_region('us-east-1')
conn = boto.connect_cloudwatch(region=region)
conn.list_metrics()
@siroken3
siroken3 / gist:3446826
Created August 24, 2012 06:47
怒られない例
#!/bin/env python
import boto.ec2.cloudwatch
region = [r for r in boto.ec2.cloudwatch.regions() if r.nmae == '<REGION-NAME>'][0]
conn = boto.connect_cloudwatch(region=region)
conn.list_metrics()
@siroken3
siroken3 / gist:3446921
Created August 24, 2012 06:59
botoで cloudwatch の RDSメトリクス取得方法
#!/bin/env python
from datetime import datetime, timedelta
end = datetime.now()
start = end - timedelta(hours=1)
data = conn.get_metric_statistics(60, start, end, "CPUUtilization","AWS/RDS","Average", { 'DBInstanceIdentifier' : 'activity' }, 'Percent')
@siroken3
siroken3 / pytimekeeper.py
Created August 28, 2012 10:54
所定時間内に終了しなかったらaws-snsで通知する関数デコレータ ref: http://qiita.com/items/c4e77bc8a3be53aacb08
# -*- coding: utf-8 -*-
import signal
import boto.sns
def timeout(limit, topic, subject='Execution Timeout.', body='Please check program.', region='us-east-1'):
'''
指定した実行時間(秒)に終了しなかった場合、awsのsnsで通知するデコレータです。
この例では long_time_func()を呼び出し後10秒経過していたら aws snsに通知が飛びます。
@timeout(limit=10,topic='arn:aws:sns:xxxxxxxx:yyyyyyy')
def long_time_func():
@siroken3
siroken3 / pytimekeeper.py
Created August 28, 2012 11:42
指定時間内に関数が終了しなかったらAWS-SNSで通知するデコレータ ref: http://qiita.com/items/9d6730acbfa4c8e0b5a4
# -*- coding: utf-8 -*-
import signal
import boto.sns
def timeout(limit, topic, subject='Execution Timeout.', body='Please check program.', region='us-east-1'):
'''
使い方:指定した実行時間に終了しなかった場合、awsのsnsで通知するデコレータです。
@timeout(limit=3600, topic='arn:aws:sns:xxxxxxxx:yyyyyyy')
def long_time_function():
very_very_long_calc()
@siroken3
siroken3 / deploy.rb
Created August 31, 2012 09:57
capistranoのrole一覧をsqliteを使って取得する ref: http://qiita.com/items/bcfa059debd45257ca4b
role(:server) do
hosts = Array.new
db = SQLite3::Database.new("serverlist.db")
db.execute("SELECT name FROM serverlist WHERE role = 'server'") do |host|
hosts.push(host[0])
end
db.close()
hosts
end
@siroken3
siroken3 / subnets.py
Created September 5, 2012 11:15
botoで任意のリージョンにあるVPCのサブネット一覧を得る
import boto
import boto.ec2
import boto.vpc
region = boto.ec2.get_region('REGION_NAME')
c = boto.connect_vpc(region=region)
subnets = c.get_all_subnets()