Ejemplo n.º 1
0
def reversion_get(reposname='', depth=10):
    """ 获取版本库当前的提交,包含编号,时间,和变化。 默认向前查10个版本库
        @Return: (status, msgs, results)
            status = INT, # 状态码, 0正常执行,否则有问题
            msgs = String, # 如果status非零, 错误信息在msgs中体现
            results = LIST, # 正常执行后的返回值,类似于 [
                    {'1':{'date':'2012-12-05 22:33:44 ()', 'diff':'更新的内容'}}
                    {'0':{'date':'2012-12-05 22:33:44 ()', 'diff':'更新的内容'}}
                ] 
    """
    if not reposname:
        return (-1, u'Error: 版本仓库名不能为空!', '')

    (status, msgs, results) = pyssh.getcmd(cmd="svnlook history " + REPOSITORY_SVN_DIR + "/" + reposname + "  | grep \/")

    if status != 0:
        return (status, msgs, results)

    reversion = [r.split() for r in results.split('\n') if r ][0:depth]

    results = []
    for r in reversion:
        (s1, m1, date) = pyssh.getcmd(cmd="svnlook date " + REPOSITORY_SVN_DIR + "/" + reposname + " -r %s"%r[0])
        (s2, m2, diff) = pyssh.getcmd(cmd="svnlook diff " + REPOSITORY_SVN_DIR + "/" + reposname + " -r %s"%r[0])
        if s1 + s2 == 0:
            results.append({r[0]:{'date':date, 'diff':diff}})

    return (status, msgs, results)
Ejemplo n.º 2
0
def get_ProcStat():
    """获取cpu监控数据
    @Return: (status, msgs, results) 
            status = INT, Function execution status, 0 is normal, other is failure.
            msgs = STRING, If status equal to 0, msgs is '', otherwise will be filled with error message.
            results = DICT {
                    "cpuuse": '34', #百分比
            }
    """
    
    status=0; msgs=''; results='';

    now_data = {} # 当前 /proc/stat 的值
    last_data = {} # 上一次 /proc/stat 的值,时间跨度足够短

    # 获取当前数据
    cmd = "cat /proc/stat | grep '%s'" % 'cpu '
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    if status == 0:
        cpu_all = results.split() 
        cpu_all.pop(0)
        cpu_data=[int(i) for i in cpu_all]
        now_data['idle']=cpu_data[3]
        now_data['total']=sum(cpu_data)
    else:
        return (status, msgs, "Error: Can not get data from file /proc/stat")
        
    # 获取历史数据
    (status, msgs, results) = pyssh.getcmd(cmd='cat /tmp/proc_stat')
    if status == 0:
        last_data = json.loads("%s" % results.strip())
    else:
        status = 0; msgs = ''; results = {}
        last_data = now_data
    
    # 保存当前数据到历史数据表中
    fp = file('/tmp/proc_stat', 'w')
    fp.write(json.dumps(now_data))
    fp.close()
    # 处理两个数据,得到要计算的值
    results = {}
    diff_total = int(now_data['total'])-int(last_data['total'])
    diff_idle=(int(now_data['idle'])-int(last_data['idle']))
    if diff_total > 0:
        results['cpuuse']=int(round(100*(float(diff_total-diff_idle)/diff_total)))
    else:
        # 第一次加载的时候,历史数据为空,无法计算, 所有初始化为0
        results['cpuuse']=0 
    return (status, msgs, results)
Ejemplo n.º 3
0
def passwd_del(username=''):
    """ 删除svn用户
        @params:username=STRING
    """
    cmd = "export HOME=/home/apache; cd "+REPOSITORY_DIR+"; htpasswd -D .htpasswd " + username
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    return (status, msgs, results)
Ejemplo n.º 4
0
def get_ProcMeminfo():
    """ 获取内存数据
    @Return: (status, msgs, results)
            status = INT, Fuction execution status, 0 is normal, other is failure.
            msgs = STRING, If status equal to 0, msgs is '', otherwise will be filled with error message.
            results = DICT {
                    'memtotal': 1017812, #单位都是 KB
                    'memused': 283708
            }
    """
    now_data = {} # 用于存储从/proc/meminfo读取的原始数据
    status=0; msgs=''; results='';
    
    #从文件获取数据信息
    cmd = "cat /proc/meminfo"
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    if status == 0:
        temps=results.split('\n')
        for temp in temps:
            tmp = temp.split()
            now_data[tmp[0]]=tmp[1]
    else:
        return (status, msgs, 'Error: Can not get data from file /proc/meminfo.')

    #处理数据得到最终结果
    results={}
    results['memtotal']=int(now_data['MemTotal:'])
    #results['memused']=int(now_data['MemTotal:'])-int(now_data['MemFree:'])-int(now_data['Buffers:'])-int(now_data['Cached:'])
    results['memused']=int(now_data['MemTotal:'])-int(now_data['MemFree:'])
    results['buffers']=int(now_data['Buffers:'])
    results['cached']=int(now_data['Cached:'])
    return (status,msgs,results)
Ejemplo n.º 5
0
def passwd_set(username='', password=''):
    """ 修改svn用户密码
        @params:username=STRING
                password=STRING
    """
    cmd = "export HOME=/home/apache; cd "+REPOSITORY_DIR+"; htpasswd -b .htpasswd " + username + " " + password
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    return (status, msgs, results)
Ejemplo n.º 6
0
def svn_get():
    """ 得到当前有那些svn仓库, 返回一个版本库名称构成的序列
        @return results=LIST        # LIST=['test1', 'test2']
    """
    cmd = "ls " + REPOSITORY_SVN_DIR
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    results = [r for r in results.split() if r]
    return (status, msgs, results)
Ejemplo n.º 7
0
def memory():
    dic={}
    (status,msg,memory)=pyssh.getcmd(cmd='/usr/bin/free -m|/bin/grep buffers/cache')
    memory=re.split(' +',memory)
    dic['total']=str(int(memory[2])+int(memory[3]))
    dic['used']=memory[2]
    dic['free']=memory[3]
    dic['use%']=str(round(float(dic['used'])*100/float(dic['total']),1))
    return dic
Ejemplo n.º 8
0
def svn_del(proname=""):
    """ 删除svn仓库
        @params: proname=STRING  #工程名,英文字母,数字,下划线
    """
    if proname != "":
        cmd = "export HOME=/home/apache; cd "+REPOSITORY_SVN_DIR+"; rm -rf " + proname
        (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    else:
        status = -1; msgs = u"不能删除空的版本库,请正确输入"; resutls = ""
    return (status, msgs, results)
Ejemplo n.º 9
0
def rootsize():
    dic={}
    (status,msg,rootsize)=pyssh.getcmd(cmd='/bin/df -h|/bin/grep "/$"')
    rootsize=rootsize.strip() 
    rootsize=re.split(' +',rootsize)
    dic['size']=rootsize[0]
    dic['used']=rootsize[1]
    dic['avail']=rootsize[2]
    dic['use%']=rootsize[3]
    return dic
Ejemplo n.º 10
0
def svn_add(proname=""):
    """ 创建svn仓库
        @params: proname=STRING  #工程名,英文字母,数字,下划线
    """
    if proname != "":
        cmd = "export HOME=/home/apache; cd "+REPOSITORY_SVN_DIR+";"
        cmd+= "svnadmin create "+proname+";"
        cmd+= "chown apache.apache -R " + proname + ";"
        cmd+= "chmod 700 -R " + proname
        (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    else:
        status = -1; msgs = u"请输入正确的版本库名称, 它有字母, 下划线, 和数字构成!"; resutls = ""
    return (status, msgs, results)
Ejemplo n.º 11
0
def passwd_get():
    """ 得到svn仓库的所有用户
        @return: results=LIST   #LIST=['user1', 'user2']
    """
    cmd = "cat " + HTPASSWD
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)

    if status != 0:
        return (status, msgs, results)
    tmp = []
    for res in results.split("\n"):
        if res != "":
            tmp.append(res.split(":")[0])
    return (status, msgs, tmp)
Ejemplo n.º 12
0
def load():
    dic={}
    (status,msg,load)=pyssh.getcmd(cmd='/usr/bin/uptime')
    load=load.strip()
    load=load.split('  ')
    load=load[-1].split(': ')
    load[1]=load[1].split(', ')
    for i in range(len(load[1])):
        if 0<float(load[1][i]) and float(load[1][i])<=1:
            load[1][i]=str(float(load[1][i])*0.6*100)
        elif 1<float(load[1][i]) and float(load[1][i])<=3:
            load[1][i]=str((float(load[1][i])+2)*20)
        elif float(load[1][i])>3:
            load[1][i]=str(100)
	load[0]=load[0].replace(" ","")
    dic[load[0]]=load[1]
    return dic
Ejemplo n.º 13
0
def repos_import_add(reposname='', dirs='{trunk,tags,branches,docs}'):
    """ 给一个版本库设置一个目录结构,默认包含{trunk,branches,tags,docs}
        @Params: reposname = STRING; #版本库名称, 是字串
                 dirs = STRING; #版本库的目录结构,多个目录, 由{}括起来, 并由,分割
                    默认是: {trunk,tags,branches,docs} 并在trunk/ 创建文件README.txt
    """
    if reposname:
        cmd = "rm -rf /tmp/repos ; "
        cmd +="mkdir -p /tmp/repos/"+dirs+" ;"
        cmd +="touch /tmp/repos/trunk/README.txt ;"
        cmd +="svn import /tmp/repos/ file://"+ REPOSITORY_SVN_DIR + "/" + reposname + "/ -m 'Set Default Directory'; "
        cmd += "rm -rf /tmp/repos "

        (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    else:
        status = -1; msgs = u'Error:版本库名称不能为空'; results = ''

    return (status, msgs, results)
Ejemplo n.º 14
0
def repos_dirtrees(reposname='', depth=2):
    """ 获取版本库的目录结构,默认深度到根之下2级
        @Params: reposname = STRING 版本库的名字
                 depth = INT 搜索的深度
        @Return: (status, msgs, results)
            status = INT, # 状态码, 0正常执行,否则有问题
            msgs = String, # 如果status非零, 错误信息在msgs中体现
            results = LIST, # 正常执行后的返回值,类似于 [
                '/', '/tree',......
            ]
    """
    if not reposname:
        return (-1, u'Error: 版本仓库名不能为空!', '')

    cmd = "svnlook tree " + REPOSITORY_SVN_DIR + "/" + reposname + " | grep \/"
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    if status != 0:
        return (status, msgs, results)

    treedirs = [t for t in results.split('\n') if t]  #  所有的目录

    index = [t.count(' ') for t in treedirs if t]     # 每个目录的级别
    
    for j in range(1, len(treedirs)):
        ct_now = treedirs[j].count(' ') # 当前目录的级别

        if ct_now > depth: # 如果超过深度, 就继续处理, 跳过当前
            treedirs[j] = ''
            continue

        for i in range(j-1,-1,-1):
            if index[i] < ct_now: # 如果当前目录的级别小于之前一个目录的,就叠加之
                treedirs[j] = treedirs[i][treedirs[i].count(' '):] + treedirs[j][treedirs[j].count(' '):] 
                ct_now = index[i]
                break
     
    treedirs = ['/'] + [t[0:-1] for t in treedirs if t and t!='/']  #  去除最后的斜线
    return (status, msgs, treedirs)
Ejemplo n.º 15
0
import pyssh

REPOSITORY_DIR = "/opt/repository"   # 仓库主目录
REPOSITORY_SVN_DIR = "/opt/repository/svnserver"   # SVN主目录
HTPASSWD = "/opt/repository/.htpasswd"      # 用户密码文件
AUTHZ = "/opt/repository/.authz.conf"       # 授权文件

if not os.path.exists('/opt'):
    os.mkdir('/opt', 0700)
if not os.path.exists(REPOSITORY_DIR):
    os.mkdir(REPOSITORY_DIR, 0700)
if not os.path.exists(REPOSITORY_SVN_DIR):
    os.mkdir(REPOSITORY_SVN_DIR, 0700)
if not os.path.exists(HTPASSWD):
    pyssh.getcmd(cmd="echo '' > %r ; chmod 700 %r"%(HTPASSWD,HTPASSWD))
if not os.path.exists(AUTHZ):
    pyssh.getcmd(cmd="echo '' > %r ; chmod 700 %r"%(AUTHZ, AUTHZ))

CF = ConfigParser.ConfigParser()
CF.read(AUTHZ)

"""
    公共返回值
    @return:status=INT,    # 执行状态, 0=成功执行函数功能, 其他数字=没有成功执行, 错误信息在msgs中
            msgs = STRING, # 执行错误的时候, 返回的错误信息
            results = INT/STRING/LIST/TUPLE/DICT, # 执行结果
"""


def svn_add(proname=""):
Ejemplo n.º 16
0
def get_ProcDiskstats(devices):
    """ 获取IO监控数据
        @Params: devices = LIST # 物理硬盘的设备名, 例如 devices=['sda', 'sdb', 'hda', 'scisi']
        @Return: (status, msgs, results)
                status = INT, Fuction execution status, 0 is normal, other is failure.
		msgs = STRING, If status equal to 0, msgs is '', otherwise will be filled with error message.
                results = DICT {
                        'sda': { # 设备sda的读写能力
                            'reading': 50032, # 单位都是  KB/s
                            'writing': 20,
                        },
                        'sdb': {
                            'reading': 50032, # 单位都是  KB/s
                            'writing': 20,
                        }
                    }

        @Note: 
            获取数据的效果, 和 #vmstat -n 1 一致
    """

    status=0; msgs=''; results='';
    if devices == []:
        return (-1, 'Error: Params is None.', '')
        

    now_data = {} # 当前 /proc/diskstats  的值
    last_data = {} # 上一次 /proc/diskstats  的值, 时间跨度由调用程序决定

    # 获取当前数据
    cmd = "cat /proc/diskstats | egrep '%s'"  % (len(devices)==1 and devices[0]+' ' or ' |'.join(devices)+' ')  # 只获取到物理硬盘的数据
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    if status == 0:
        now_data['timestamp'] = time.time()
        temps = results.split('\n')
        for temp in temps:
            tmp = re.split('[ ]+', temp)
            now_data[tmp[3]] = {
                    'number_of_issued_reads':tmp[4], # Field 1  
                    'number_of_reads_merged':tmp[5], # Field 2
                    'number_of_sectors_read':tmp[6], # Field 3
                    'number_of_milliseconds_spent_reading':tmp[7],    # Field 4    
                    'number_of_writes_completed':tmp[8],      # Field 5
                    'number_of_writes_merged':tmp[9],         # Field 6
                    'number_of_sectors_written':tmp[10],       # Field 7
                    'number_of_milliseconds_spent_writing':tmp[11],    # Field 8
                    'number_of_IOs_currently_in_progress':tmp[12],    # Field 9
                    'number_of_milliseconds_spent_doing_IOs':tmp[13],    # Field 10
                    'number_of_milliseconds_spent_doing_IOs_2':tmp[13],    # Field 11
                }
    else:
        return (status, msgs, 'Error: Can not get data from file /proc/diskstats.')

    # 获取历史数据
    (status, msgs, results) = pyssh.getcmd(cmd='cat /tmp/proc_diskstats')
    if status == 0 and results!= '':
        last_data = json.loads("%s" % results.strip())
    else:
        status = 0; msgs = ''; results = {}
        last_data = now_data

    # 保存当前数据到历史数据表中
    #cmd = """echo \"%s\" > /tmp/proc_diskstats""" % json.dumps(now_data)
    #(status, msgs, results) = pyssh.getcmd(cmd=cmd)
    fp = file('/tmp/proc_diskstats', 'w')
    fp.write(json.dumps(now_data))
    fp.close()

    # 处理两个数据,得到要计算的值
    results = {}
    timecut = float(now_data['timestamp']) - float(last_data['timestamp'])
    if timecut > 0:
        for key in devices:
            reading = (int(now_data[key]['number_of_sectors_read']) - int(last_data[key]['number_of_sectors_read']))/2/timecut
            writing = (int(now_data[key]['number_of_sectors_written']) - int(last_data[key]['number_of_sectors_written']))/2/timecut
            results[key] = {'reading':int(reading), 'writing':int(writing)}
    else:
        # 第一次加载的时候,历史数据为空, 无法计算,所以初始化为0
        for key in devices:
            results[key] = {'reading':0, 'writing':0}

    return (status, msgs, results)
Ejemplo n.º 17
0
def get_ProcNetDev(devices, bandwidth):
    """获取网络监控数据
        @Params: devices = LIST # 物理网卡的设备名,例如 devices=['eth0', 'eth1', 'eth2', 'eth3']
                 bandwidth = DICT # 对应物理网卡的带宽,例如 bandwidth={'eth0':1000, 'eth1':10000})
        @Return: (status, msgs, results) 
            status = INT, Fuction execution status, 0 is normal , other is failure.
            msgs = STRING, If status equal to 0, msgs is '', otherwise will be filled with error message.
            results = DICE {
                    'eth0': { #网卡eth0的传输能力
                        'transmit': 1024, #单位无特殊说明都是 KB/s
                        'receive': 10240, 
                        'bandwidth':1000, #单位是 M/s
                    },
                    'eth1': { #网卡eth1的传输能力
                        'transmit': 1024, #单位无特殊说明都是 KB/s
                        'receive': 10240, 
                        'bandwidth':1000, #单位是 M/s
                    }
                }
        @Note:
    """

    status=0; msgs=''; results='';
    if devices == []:
        return (-1, 'Error: Params is None.', '')
    now_data = {} # 当前 /proc/net/dev 的值
    last_data = {} # 上一次 /proc/net/dev 的值,时间跨度由调用程序决定

    # 获取当前数据
    cmd = "cat /proc/net/dev | egrep '%s'"  % (len(devices)==1 and devices[0]+':' or ':|'.join(devices)+':') #获取物理网卡的数据
    (status, msgs, results) = pyssh.getcmd(cmd=cmd)
    if status == 0:
        now_data['timestamp'] = time.time()
        temps = results.split('\n')
        for temp in temps:
            tmp_main = temp.split(':')
            tmp=tmp_main[1].split()
            now_data[tmp_main[0].strip()] = {
                'receive_bytes': tmp[0],
                'receive_packets': tmp[1],
                'receive_errs': tmp[2],
                'receive_drop': tmp[3],
                'receive_fifo': tmp[4],
                'receive_frame': tmp[5],
                'receive_compressed': tmp[6],
                'receive_multicast': tmp[7],
                'transmit_bytes': tmp[8],
                'transmit_packets': tmp[9],
                'transmit_errs': tmp[10],
                'transmit_drop': tmp[11],
                'transmit_fifo': tmp[12],
                'transmit_colls': tmp[13],
                'transmit_carrier': tmp[14],
                'transmit_compressed': tmp[15]
            }
    else:
        return (status, msgs, 'Error: Can not get data from file /proc/net/dev.')
    
    # 获取历史数据
    (status, msgs, results) = pyssh.getcmd(cmd="cat /tmp/proc_net_dev")
    if status == 0:
        last_data = json.loads("%s" % results.strip())
    else:   # 第一次加载,没有历史数据的情况
        status=0; msgs = ''; results = {}
        last_data = now_data
    fp = file('/tmp/proc_net_dev', 'w')
    fp.write(json.dumps(now_data))
    fp.close()

    # 处理两个数据,得到要计算的值
    results = {}
    timecut = float(now_data['timestamp']) - float(last_data['timestamp'])
    if timecut > 0:
        for key in devices:
            receive = (int(now_data[key]['receive_bytes']) - int(last_data[key]['receive_bytes']))/1024/timecut 
            transmit = (int(now_data[key]['transmit_bytes']) - int(last_data[key]['transmit_bytes']))/1024/timecut 
            results[key] = {'receive':int(receive), 'transmit':int(transmit), 'bandwidth':bandwidth[key]}
    else:
        # 第一次加载的时候,历史数据为空,无法计算,所以初始化为0
        for key in devices:
            results[key] = {'receive':0, 'transmit':0, 'bandwidth':bandwidth[key]}
    return (status,msgs,results)