docker : 容器连接 [TODO]

1. 父容器 跟 子容器相连接

注:你可以使用一个父容器连接多个子容器。例如,我们可以有多个web容器连接到我们的db数据库容器。

参考:
http://wiki.jikexueyuan.com/project/docker/userguide/dockerlinks.html

jenkins 二次开发

1

 

参考:

1.https://github.com/openshift/jenkins
2.https://github.com/blackye/Jenkins
3.https://github.com/chef-cookbooks/jenkins
4.https://github.com/kohsuke/jenkins
5.https://github.com/search?utf8=%E2%9C%93&q=jenkins&type=

linux : Nagios 部署

1.

 

参考:

  1. http://www.jikexueyuan.com/course/1731.html
  2. https://my.oschina.net/dyyweb/blog/753155
  3. http://www.apelearn.com/bbs/study/24.htm
  4. http://xiangpang.blog.51cto.com/10719398/1710727
  5. https://www.howtoforge.com/nagios-on-ubuntu-14.04-trusty-tahr-and-debian-7-wheezy
  6. https://www.digitalocean.com/community/tutorials/how-to-install-nagios-4-and-monitor-your-servers-on-ubuntu-14-04

linux : 磁盘监控脚本

 

1.

#coding:utf-8
import os
import re
import smtplib
import datetime
import shelve
from email.mime.text import MIMEText

# 硬盘使用率报警阀值
hd_usage_rate_threshold = 80

# 要发给谁
mailto_list=["******@17guagua.com","******@17guagua.com"]

# 设置服务器,用户名、口令以及邮箱的后缀
mail_host="smtp.17guagua.com"
mail_user="******@17guagua.com"
mail_pass="******"
mail_postfix="17guagua.com"

# 日志偏移
log_offset = shelve.open('log_offset')

# 取当天日期
log_path_suffix=(datetime.date.today()).strftime('%Y-%m-%d')

# 当前日期key
cur_time = 'cur_time'

# 日志路径
app_info = {}
app_info['event'] = ['/opt/log/guagua_web_event_extends/event-ext-'+log_path_suffix+'.log',['失败','异常'],[]]

# 处理日志
def analysis_log(appName ,appInfo):
    cur_time_val = get_shelve_value(cur_time)
    if cur_time_val == -1:
        set_shelve_value(cur_time, log_path_suffix)
    elif log_path_suffix != cur_time_val:
        set_shelve_value(appName, 0)
        set_shelve_value(cur_time, log_path_suffix)

    f1 = file(appInfo[0], "r")
    offset = get_shelve_value(appName)
    if offset != -1:
        f1.seek(offset,1)
    else:
        set_shelve_value(appName, 0)
    count = 0
    exceptionStr = ""
    for s in f1.readlines():
        searchKey = appInfo[1]
        if len(searchKey) > 0:
            for i in searchKey:
                li = re.findall(i, s)
                if len(li) > 0:
                    count = count + li.count(i)
                    exceptionStr = exceptionStr + " " + s
        else:
            li = re.findall('Exception', s)
            if len(li) > 0:
                count = count + li.count('Exception')
                exceptionStr = exceptionStr + " " + s
    set_shelve_value(appName, f1.tell())
    print appName + " 异常数量为 " + str(count)
    return [count, "---------------------------------" + appName + " ----------------------------- \n " + exceptionStr]

#shelve 处理
def set_shelve_value(key, value):
    log_offset[key] = value

def get_shelve_value(key):
    if log_offset.has_key(key):
        return log_offset[key]
    else:
        return -1

def del_shelve_value(key):
    if log_offset.has_key(key):
        del log_offset[key]

# 发送邮件
def send_mail(to_list,sub,content):
  me = mail_user + "<"+ mail_user + "@" + mail_postfix + ">"
  msg = MIMEText(content, 'html', 'utf-8')
  msg['Subject'] = sub
  msg['From'] = me
  msg['To'] = ";".join(to_list)
  try:
    s = smtplib.SMTP()
    s.connect(mail_host)
    s.login(mail_user,mail_pass)
    s.sendmail(me, to_list, msg.as_string())
    s.close()
    return True
  except Exception, e:
    print str(e)
    return False

# 获得外网ip
def get_wan_ip():
    cmd_get_ip = "/sbin/ifconfig |grep 'inet addr'|awk -F\: '{print $2}'|awk '{print $1}' | grep -v '^127' | grep -v '192'"
        get_ip_info = os.popen(cmd_get_ip).readline().strip()
    return get_ip_info

# 检测硬盘使用
def check_hd_use():
        cmd_get_hd_use = '/bin/df'
        try:
            fp = os.popen(cmd_get_hd_use)
        except:
            ErrorInfo = r'get_hd_use_error'
            print ErrorInfo
            return ErrorInfo
        re_obj = re.compile(r'^/dev/.+\s+(?P<used>\d+)%\s+(?P<mount>.+)')
        hd_use = {}
        for line in fp:
            match = re_obj.search(line)
            if match is not None:
                hd_use[match.groupdict()['mount']] = match.groupdict()['used']
        fp.close()
        return  hd_use

# 硬盘使用报警
def hd_use_alarm():
    for v in check_hd_use().values():
    if int(v) > hd_usage_rate_threshold:
        if send_mail(mailto_list,
                 'System Disk Monitor',
             'nSystem Ip:%s\nSystem Disk Use:%s'
             % (get_wan_ip(),check_hd_use())):
             print  "sendmail success!!!!!"
    else:
         print "disk not mail"

if __name__ == '__main__':
  hd_use_alarm()
  exceptionCount = 0
  exceptionContents = "";
  for key in app_info:
    exceptionContent = analysis_log(key, app_info[key])
    exceptionCount += exceptionContent[0]
    exceptionContents += exceptionContent[1]
    exceptionContents = exceptionContents + "*********************************************** \n"

  print exceptionCount
  if exceptionCount > 0:
    if send_mail(mailto_list, get_wan_ip() + " 日志报警",exceptionContents):
        print "发送成功"
    else:
        print "发送失败"

 

参考:

  1. http://blog.csdn.net/daidaineteasy/article/details/48792425

linux : 内存监测

1.

I recommend combining the previous answers

watch -n 5 free -m

Note that Linux likes to use any extra memory to cache hard drive blocks. So you don’t want to look at just the free Mem. You want to look at the free column of the -/+ buffers/cache: row. This shows how much memory is available to applications. So I just ran free -m and got this:

             total       used       free     shared    buffers     cached
Mem:          3699       2896        802          0        247       1120
-/+ buffers/cache:       1528       2170
Swap:         1905         62       1843

I know that I’m using 1528 MB and have 2170 MB free.

Note: To stop this watch cycle you can just press Ctrl+C.

 

2.cat /proc/meminfo

$ cat /proc/meminfo
MemTotal: 23255916 kB
MemFree: 4042492 kB
MemAvailable: 19533036 kB
Buffers: 2116372 kB
Cached: 11157484 kB
SwapCached: 0 kB
Active: 10161308 kB
Inactive: 6311148 kB
Active(anon): 3203484 kB
Inactive(anon): 21316 kB
Active(file): 6957824 kB
Inactive(file): 6289832 kB
Unevictable: 4976 kB
Mlocked: 4976 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 40 kB
Writeback: 0 kB
AnonPages: 3203640 kB
Mapped: 199176 kB
Shmem: 22632 kB
Slab: 2423408 kB
SReclaimable: 2315020 kB
SUnreclaim: 108388 kB
KernelStack: 13120 kB
PageTables: 38708 kB

3. htop 的使用

http://saiwei.me/wp-admin/post.php?post=3718&action=edit

 

参考:

  1. http://askubuntu.com/questions/9642/how-can-i-monitor-the-memory-usage

shell : 自动删除过期文件

1.

linux是一个很能自动产生文件的系统,日志、邮件、备份等。所以自动删除就显的很方便。
语句写法:
find 对应目录 -mtime +天数 -name “文件名” -exec rm -rf {} \;
例1:
find /u01/backups -mtime +10 -name “*.*” -exec rm -rf {} \;
将/u01/backups目录下所有10天前带”.”的文件删除
find:linux的查找命令,用户查找指定条件的文件
/u01/backups:想要进行清理的任意目录
-mtime:标准语句写法
+10:查找10天前的文件,这里用数字代表天数,+30表示查找30天前的文件
“*.*”:希望查找的数据类型,”*.jpg”表示查找扩展名为jpg的所有文件,”*”表示查找所有文件
-exec:固定写法
rm -rf:强制删除文件,包括目录
{} \; :固定写法,一对大括号+空格+\+;

可以将这小语句写到一个可执行文件中,再设置cron调度执行,那就可以让系统自动去清理相关文件。

新建一个可执行文件clear.sh
#vi clear
#!/bin/sh
find /u01/backups -mtime +10 -name “*.*” -exec rm -rf {} \;

赋权
#chmod 777 clear
将clear.sh文件加入到系统计划任务,到点自动执行
#crontab -e
* 2 * * */usr/local/bin/clear
这里的设置是每天凌晨2点执行clear文件进行数据清理

 

参考:http://www.lai18.com/content/3220570.html