以后我代码的log, 嘿嘿

code

(更多…)

十二月 25th, 2013

Posted In: python

使用salt收集minion信息时,默认items的信息有些不是我们想要的,有些想要的又没有,就扩展下了grains的方法,呵,感谢还不错

1

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#=============================================================================
#     FileName:
#         Desc:
#       Author: 苦咖啡
#        Email: voilet@qq.com
#     HomePage: http://blog.kukafei520.net
#      Version: 0.0.1
#   LastChange: 
#      History:
#=============================================================================
from __future__ import print_function
from collections import namedtuple

import os
import subprocess
import re
import hashlib
#对字典取子集
def sub_dict(form_dict, sub_keys, default=None):
    return dict([(k, form_dict.get(k.strip(), default)) for k in sub_keys.split(',')])
#读取cpuinfo信息
# dmidecode -t 4
def read_cpuinfo():
    cpu_stat = []
    with open('/proc/cpuinfo', 'r') as f:
        data = f.read()
        for line in data.split('\n\n'):
            cpu_stat.append(line)
    return cpu_stat[-2]
 (更多…)

十二月 12th, 2013

Posted In: python

内存信息 / meminfo
返回dict

#!/usr/bin/env python 
def memory_stat(): 
    mem = {} 
    f = open("/proc/meminfo") 
    lines = f.readlines() 
    f.close() 
    for line in lines: 
        if len(line) < 2: continue 
        name = line.split(':')[0] 
        var = line.split(':')[1].split()[0] 
        mem[name] = long(var) * 1024.0 
    mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached'] 
    return mem

CPU信息 / cpuinfo
返回list,每核心一dict

#!/usr/bin/env python 
def cpu_stat(): 
    cpu = [] 
    cpuinfo = {} 
    f = open("/proc/cpuinfo") 
    lines = f.readlines() 
    f.close() 
    for line in lines: 
        if line == 'n': 
            cpu.append(cpuinfo) 
            cpuinfo = {} 
        if len(line) < 2: continue 
        name = line.split(':')[0].rstrip() 
        var = line.split(':')[1] 
        cpuinfo[name] = var 
    return

(更多…)

十二月 12th, 2013

Posted In: python

最近写python常用到时间和日期格式的问题,在网上看到一大神写了点片段,调试完总算满足了自己的需求,相信很多朋友也有这方面的需求,在此发出来和大家分享

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#=============================================================================
#     FileName:
#         Desc:
#       Author: 苦咖啡
#        Email: voilet@qq.com
#     HomePage: http://blog.kukafei520.net
#      Version: 0.0.1
#   LastChange: 
#      History:
#=============================================================================

import datetime

date1 = datetime.datetime.now()
this_week_start_dt = str(date1-datetime.timedelta(days=date1.weekday())).split()[0]
this_week_end_dt = str(date1+datetime.timedelta(days=6-date1.weekday())).split()[0]
print this_week_start_dt,this_week_end_dt

 (更多…)

十二月 11th, 2013

Posted In: python

最近一直在研究saltstack,在此感谢”绿肥@灿哥”和saltstack群里的朋友

saltstack地址http://wiki.saltstack.cn/

1

(更多…)

十二月 11th, 2013

Posted In: linux系统

无觅相关文章插件,快速提升流量