您的位置:首页 > 运维架构 > Linux

Linux系统管理之技巧—— 2017-03-23

2017-03-23 14:01 239 查看
项目完成了一个阶段性的任务,在这里小结一下几个小技巧:
授权研发人员权限越级(sudo)

$ cat public
User_Alias A4 = public
A4       ALL=(ALL)       NOPASSWD: /usr/bin/vim,/bin/cat,/usr/bin/tail  #这里你自己定义
$ ansible all -m copy -a 'src=public dest=/etc/sudoers.d' -s
2. 批量memcached清除缓存(flush_all)
echo 'flush_all' | nc -z -w 1 $IP $PORT
如果有很多,可以向下罗列,批量清除,如果你觉得很low,可以使用数组declare -a,或者循环while,
写python当然也行,附上自己的过程,请参考:
$ cat clear_memcached.py
from pymemcache.client.base import Client
import sys

def read_log(path):
with open(path) as f:
yield from f

def HP(path):
for line in read_log(path):
ret = line.strip().split()
ret[1] = int(ret[1])
ret = tuple(ret)
print(ret)
yield ret

def clear_mem(path):
for hp in HP(path):
c.flush_all()
c.close()

if __name__ == "__main__":
clear_mem(sys.argv[1])

$ cat mall_mem.txt
$IP1 $PORT1
$IP1 $PORT2
...
3. 远程快速校验多台主机不同目录下的文件内容一致性(扩容时发挥作用很大)
$ ssh -t $ip 'find $dir1 $dir2 ... -type f -exec md5sum {} \;' > $ip.md5 # 注意$dir1..等使用绝对路径
$ doc2unix $ip.md5  #(这个很隐秘,多了windows的回车符)
$ ssh -tt $ip.other 'md5sum -c --quiet' < $ip.md5
# 如果一致,什么也不反回;不一致的会告诉NOT MATCH的文件
哎,非逼自己用python去写,实现了find + sha256sum,但想将其保存到特定文件中,这一点却没实现。这一版,只是想回顾“装饰器”,这里没起到作用。
#!/usr/bin/env python

import os, os.path, sys
import hashlib
from functools import wraps

def search(fn):
@wraps(fn)
def wrap(*args, **kwargs):
paths = list(args)
ret = fn(*args, **kwargs)
#paths.pop()
for path in paths:
print(path)
for pathname in os.listdir(path):
pathname = os.path.join(path, pathname)
if os.path.isfile(pathname):
with open(pathname, 'rb') as f:
m = hashlib.sha256(f.read())
# hashfile = path + '.sha256'
# print(hashfile)
# with open(hashfile, 'a+') as f:
#     f.write('{}  {}\n'.format(m.hexdigest(), pathname))
print('%s  %s' % (m.hexdigest(), pathname))
#ret.write('aaa')
#ret.write('%s  %s' % (m.hexdigest(), pathname))
#ret.close()
if os.path.isdir(pathname):
wrap(pathname)
return wrap

@search
def write(*args, **kwargs):
pass
#hashfile = list(args).pop()
#print(hashfile)
#hashfile = '10.255.201.10'
#f = open(hashfile, 'a+')
#return f

if __name__ == '__main__':
write(*sys.argv[1:])
4. 配置Open-falcon HostGroups时,根据hostname绑定template,快速获取平台的主机名
$ ansible $group1 -m setup -a 'filter=ansible_hostname' -o | awk -F'[ :|"{}]' '{print $17}'


5. 关闭远程Nginx\Tomcat进程,启动服务就不写了
$ cat marketapi_stop.sh
#!/bin/bash
kill -QUIT $(cat /home/aspire/config/nginx/nginx.pid)
$ ansible $group1 -m script -a 'marketapi_stop.sh' -s
-------
$ cat tomcat_stop.sh
#!/bin/bash
kill -9 $(ps aux | awk '/tomat808[0]\/conf/{print $2}')
$ ansible $group1 -m script -a 'tomcat_stop.sh' -s
6. 远程tail -f 查看日志滚动
$ ssh -t $IP 'tail -f xxxx.log'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  技巧