AWS Cloudwatch query script for Zabbix

Standard
#!/usr/bin/python
import boto.ec2.cloudwatch
import sys
import datetime
 
try:
    metName = sys.argv[1]
    funcName = sys.argv[2]
    dimSpace = sys.argv[3]
    region = sys.argv[4]
    accessKey = sys.argv[5]
    secretKey = sys.argv[6]
 
except:
    print "Usage: GetFromCloudwatch.py MetricName Function Dimension Region AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY"
    print "Example: GetFromCloudwatch.py FreeableMemory Average \"CacheClusterId=ElsticCacheName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    print "         GetFromCloudwatch.py CPUUtilization Average \"DBInstanceIdentifier=RDSName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    print "         GetFromCloudwatch.py ApproximateNumberOfMessagesVisible Average \"QueueName=SQSName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    sys.exit(1)
 
dim = {}
firstSplit = dimSpace.split(',')
for word in firstSplit:
    secondSplit = word.split('=')
    dim[secondSplit[0]] = secondSplit[1]
 
regions = boto.ec2.cloudwatch.regions()
 
reg = ''
for r in regions:
    if region == r.name:
        reg = r
 
c = boto.ec2.cloudwatch.CloudWatchConnection(aws_access_key_id=accessKey, aws_secret_access_key=secretKey, region=reg)
metrics = c.list_metrics(dimensions=dim)
 
end = datetime.datetime.utcnow()
start = end - datetime.timedelta(minutes=15)
 
dataPoints = [];
for met in metrics:
    if met.name == metName:
        dataPoints = met.query(start, end, funcName)
 
if len(dataPoints) > 0:
    max = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
    index = 0
    for i in range(0,len(dataPoints)):
        if max < dataPoints[i][u'Timestamp']:
            max = dataPoints[i][u'Timestamp']
            index = i
    for key in dataPoints[index].keys():
        if funcName in key:
            value = dataPoints[index][key]
    print value
else:
    print 'Error! No response from Amazon.'
    sys.exit(2)

Werkzeug Tutorial

Standard

原文:http://werkzeug.pocoo.org/docs/0.10/tutorial/


Please keep in mind that Werkzeug is not a framework, it’s a library with utilities to create your own framework or application and as such is very flexible.

python英文翻译

Standard

之前在网上下了个mp3格式的英语高频字汇表,带lrc字幕的.
平时没事可以一直开着加深记忆.
不过其字幕没有中文翻译,于是就写了段东西帮它翻译一下.

原字幕文件

[00:00.01]a
[00:01.728]abbreviation
[00:04.128]able
[00:05.976]about
[00:08.16]abroad
[00:09.864]absent-minded
[00:12.336]absorb

用于翻译英文单词的URL返回值
URL = ‘http://dict.cn/ws.php?utf8=true&q=%s’

<?xml version="1.0" encoding="UTF-8" ?>
<dict>
<key>a</key>
<lang>ec</lang>
<audio>http://mp3.dict.cn/mp3.php?q=JIkye</audio>
<pron>&#601;,ei</pron>
<def>art.一(个), 每一(个), 任一(个) </def>
<sent><orig>I have engaged &lt;em&gt;a&lt;/em&gt; room at this hotel.</orig><trans>我已经在这个旅馆里预订了一个房间。</trans></sent>
 
<sent><orig>You should give it &lt;em&gt;a&lt;/em&gt; try.</orig><trans>你应该试一试。</trans></sent>
<sent><orig>I don't care what &lt;em&gt;a&lt;/em&gt; car looks like as long as it gets me from A to B.</orig><trans>我倒不在乎汽车的样子,只要能把我从一处载到另一处就行了。</trans></sent>
</dict>

代码

#!/usr/bin/python
#coding=utf8
 
import urllib
import xml.dom.minidom as xml
 
URL = 'http://dict.cn/ws.php?utf8=true&q=%s'
 
def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc
 
def translate(word):
    xmls = urllib.urlopen(URL % urllib.quote(word)).read()
    root = xml.parseString(xmls).documentElement
 
    try:
        str = getText(root.getElementsByTagName("def")[0].childNodes)
    except:
        str = "NULL"
    return str.replace('\n','')
 
if __name__ == '__main__':
    r=open('english.lrc')
    wordlist=list( r.read().split('\n') )
    r.close()
 
    w=open('english_new.lrc','a')
 
    i=0
    while i<len(wordlist):
        text = wordlist[i].split(']')[0]+"]"+wordlist[i].split(']')[1] + "  " + translate( wordlist[i].split(']')[1])
        w.writelines(text.encode("utf-8")+'\n\r')
        i=i+1
 
    w.close();

翻译后的字幕

[00:00.01]a  art.一(个), 每一(个), 任一(个) 
[00:01.728]abbreviation  n.缩写 
[00:04.128]able  adj.能够的, 有能力的, 能干的 
[00:05.976]about  prep.关于, 大约, 在 ... 周围 adv.大约, 附近, 到处, 在周围 
[00:08.16]abroad  adv.到国外, 广为流传 adj.在国外, 海外(一般作表语) 
[00:09.864]absent-minded  adj. 心不在焉的
[00:12.336]absorb  vt.吸收, 吸引 ... 的注意, 吞并, 承受

python pexpect多机远程管理范例

Standard
#!/usr/bin/python
 
import pexpect
import sys
import time
import os
 
def ssh_cmd(ip, user, pwd, cmds):
  try:
    foo = pexpect.spawn('ssh %s@%s' % (user,ip))
    index = foo.expect(['Password:', 'yes/no'])
 
    if index == 0:
      foo.sendline(pwd)
    elif index == 1:
      foo.sendline('yes')
 
    foo.expect('~ #')
 
    for cmd in cmds.split(","):
      foo.sendline(cmd)
 
  except pexpect.EOF:
    foo.close()
  else:
    foo.close
  return ip
 
file = open('serverlist.txt')
try:
  hosts = file.read()
finally:
  file.close()
 
  for host in hosts.split("\n"):
    if host:
      ip,user,pwd,cmds = host.split(":")
      print ssh_cmd(ip,user,pwd,cmds)
172.28.201.101:yemaosheng:com:echo '1' >> /root/1,echo '11' >> /root/2
172.28.201.102:yemaosheng:com:echo '2' >> /root/2
172.28.201.103:yemaosheng:com:echo '3' >> /root/3

python多线代理抓取验证范例

Standard

之前批量管理机器都直接用shell,最近也想赶赶时髦试着用python来写写看.
找了个多线代理抓取验证的小程序,以此做范例来学习理解python.里面涉及到多线程及python的基础,看一遍理解了基本上也就算入门了.
这个程序不是我写的,我只是稍微改了下再加了点注释,原作者和出处我在网上也没能搜不到-_-

# -*- coding: utf-8 -*-
import urllib2,re,thread,time
import socket
socket.setdefaulttimeout(10)
#------------------从网上抓取IP地址------------------#
def getcnproxy(name):
    pagenum=0 #页码初始0
    result=[] #结果集
    trycount=0 #失败尝试次数
    while trycount<=3 and pagenum<1: #失败3次就停止抓取并且只抓1页
        pagenum=pagenum+1
        url='http://www.proxycn.com/html_proxy/http-'+str(pagenum)+'.html'
        #http://www.proxycn.com/html_proxy/http-1.html'
        try:
            html=urllib2.urlopen(url) #打开链接
            ip=''
            for line in html:
                if '''onDblClick="clip''' in line: #页面中IP地址这行包含onDblClick="clip,所以当判定到它存在即往下提取出IP地址
                    proxy=line[line.find("clip('")+6:line.find("')")] #clip('和')是网页中IP地址的前后html代码
                    lock.acquire()
                    print(name,'-',proxy)
                    lock.release()
                    result.append(proxy) #将结果放入result[]
        except:
            trycount=trycount+1
            pagenum=pagenum-1
    proxylist[0]=result #proxylist是函数外的一个变量
    return result
 
def getproxycn(name):
    pagenum=0
    result=[]
    getallpages=0
    trycount=0
    while pagenum<=9 and trycount<=2 and pagenum<1:
        pagenum=pagenum+1
        url='http://www.cnproxy.com/proxy'+str(pagenum)+'.html'
        try:
            html=urllib2.urlopen(url)
            for line in html:
                if "HTTP" in line:
                    proxy=line[line.find('<td>')+6:line.find('<SCRIPT type')]
                    lock.acquire()
                    print(name,'-',proxy)
                    lock.release()
                    result.append(proxy)
        except:
            trycount=trycount+1
            pagenum=pagenum-1
    proxylist[1]=result
    return result
#--------------------------------------------------#
#------------------代理验证------------------#
def proxycheckone(proxy):
    url='http://www.google.com/'
    proxy_url = 'http://'+proxy
    proxy_support = urllib2.ProxyHandler({'http': proxy_url}) #设定使用proxy
    opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
    r=urllib2.Request(url)
    r.add_header("Accept-Language","utf-8") #加入头信息,这样可避免403错误
    r.add_header("Content-Type","text/html; charset=utf-8")
    r.add_header("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)")
    trycount=1
    while trycount<=2: #尝试2次
        try:
            T0=time.time() #开始时间
            f=opener.open(r)
            data=f.read()
            if 'Google' in data: #判断到网页信息中有Google字符,说明通过此proxy连接google可以成功
                T=time.time()-T0 #得出最终连接所用时间
                break
            else:return []
        except:
            time.sleep(1)
            trycount=trycount+1
    if trycount>2:
        return []
    else:
        return '地址:'+proxy+' 连接速度:'+str(T) #此信息会写入一个文本文件
 
def proxycheck(idnum):
    while 1:
        r.acquire()
        try:
            i=proxylist[0]
            del proxylist[0]
            r.release()
        except:
            r.release()
            x[idnum]=1
            break
        b=proxycheckone(i)
        if len(b)>0:
            a.acquire()
            y.append(b) #写入可用代理IP
            a.release()
#-------------------------------------------#
if __name__ == '__main__':
    #------------------将得到的代理IP地址写入文件------------------#
    lock=thread.allocate_lock()
    proxylist=[[],[]] #用于存放下面两函数运行时返回的结果
    thread.start_new(getcnproxy,('cnproxy',)) #开线程运行getcnproxy函数,参数值cnproxy
    thread.start_new(getproxycn,('proxycn',)) #开线程运行getproxycn函数,参数值proxycn
    while [] in proxylist:
        time.sleep(1)
    proxylist=proxylist[0]+proxylist[1]
    w=open('proxies.txt','a') #新建文件
    w.write('\n'.join(proxylist)) #写入抓取到的IP
    w.close()
    del proxylist #清掉数组
    print('get all proxies!\n\n')
    #-----------------------------------------------------------#
    #------------------从文件中取出代理IP并验证------------------#
    w=open('proxies.txt') #打开文件
    proxylist=list(set((re.sub(r'(\t+[^\n]*\n|\n)',',',w.read())).split(','))) #通过正则来得到IP
    while '' in proxylist:
        del proxylist[proxylist.index('')]
    w.close()
 
    lock=thread.allocate_lock()
    r=thread.allocate_lock()
    a=thread.allocate_lock()
    y=[]
    x=[0]*120
 
    for idnum in range(0,100):
        thread.start_new(proxycheck,(idnum,))#线程运行proxycheck函数,参数值idnum 0-100
 
    while 0 in x:
        print("Remain:",len(proxylist)," Finish:",sum(x)," Check OK:",len(y))
        time.sleep(10)
 
    w=open('proxies.txt','w')
    w.write(re.sub('^\n','',re.sub(r'\n+','\n','\n'.join(y)+'\n')))
    w.close()
    #---------------------------------------------------------#