小谢同学写的Linux 服务器调优脚本

modify_sysctl.py

[code lang=python] # -- coding: utf-8 -- ‘’’ Created on 2015-1-21 @author: xie ‘’’ ‘’’ net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 30000 net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 9000 65535 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_orphans = 262144 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 ‘’’ import re,commands SYSCTL_CONFIG = ‘/etc/sysctl.conf’ class SetSysctl(): def __init__(self): self._sysctls = [] self._sysctls.append(‘net.ipv4.tcp_rmem = 4096 87380 16777216’) self._sysctls.append(‘net.ipv4.tcp_wmem = 4096 65536 16777216’) self._sysctls.append(‘net.core.wmem_default = 8388608’) self._sysctls.append(‘net.core.rmem_default = 8388608’) self._sysctls.append(‘net.core.rmem_max = 16777216’) self._sysctls.append(‘net.core.wmem_max = 16777216’) self._sysctls.append(‘net.core.netdev_max_backlog = 30000’) self._sysctls.append(‘net.core.somaxconn = 65535’) self._sysctls.append(‘net.ipv4.tcp_max_syn_backlog = 262144’) self._sysctls.append(‘net.ipv4.tcp_max_tw_buckets = 6000’) self._sysctls.append(‘net.ipv4.tcp_tw_recycle = 1’) self._sysctls.append(‘net.ipv4.tcp_tw_reuse = 1’) self._sysctls.append(‘net.ipv4.tcp_fin_timeout = 30’) self._sysctls.append(‘net.ipv4.ip_local_port_range = 9000 65535’) self._sysctls.append(‘net.ipv4.tcp_syncookies = 1’) self._sysctls.append(‘net.ipv4.tcp_max_orphans = 262144’) self._sysctls.append(‘net.ipv4.tcp_synack_retries = 2’) self._sysctls.append(‘net.ipv4.tcp_syn_retries = 2’) self._sysctls.append(‘’) self._sysctls.append(‘kernel.core_pattern = /home/core/core.%p’) self._create_core() self._read_sysctl() self._write_sysctl() def _read_sysctl(self): network = open(SYSCTL_CONFIG, ‘r’) self._lines = [] for line in network: flag = False for sysctl in self._sysctls: regex_str = sysctl.split(‘=’)[0] ma = re.match(r’^‘+regex_str+’.*?‘, line, re.IGNORECASE) if ma: flag = True break; if not flag: self._lines.append(line) network.close() def _write_sysctl(self): sysctls = open(SYSCTL_CONFIG, ‘w’) self._lines.extend(self._sysctls) sysctls.write(’\n’.join(self._lines)) sysctls.close() commands.getoutput(‘sysctl -p’) def _create_core(self): commands.getoutput(‘mkdir /home/core’) commands.getoutput(‘chown -R mc-ops:mc-ops /home/core’) if __name__ == ‘__main__’: SetSysctl() [/code]

modify_ulimit.py

[code lang=python]
# -- coding: utf-8 -- #!/usr/bin/env python2.7 ‘’’ Created on 2013-5-6 @author: xie ‘’’ import commands, re,os LIMIT_CONF = ‘/etc/security/limits.conf’ SOFT_NOFILE = ‘* soft nofile 1048576\n’ HAND_NOFILE = ‘* hard nofile 1048576\n’ SOFT_NPROC = ‘* soft nproc 1048576\n’ HAND_NPROC = ‘* hard nproc 1048576\n’ PROFILE_CONF = ‘/etc/profile’ ULIMIT_NOFILE = ‘ulimit -HSn 1048576\n’ NPROC_LIMIT_CONF = ‘/etc/security/limits.d/90-nproc.conf’ def modify_ulimit(): lines = [] ulimit = open(LIMIT_CONF, ‘r’) for line in ulimit: ma = re.match(r’.?soft ?‘, line, re.IGNORECASE) mx = re.match(r’.?hard ?‘, line, re.IGNORECASE) if ma or mx: continue lines.append(line) ulimit.close() lines.append(SOFT_NOFILE) lines.append(HAND_NOFILE) lines.append(SOFT_NPROC) lines.append(HAND_NPROC) ulimit = open(LIMIT_CONF, ‘w’) ulimit.write(’‘.join(lines)) ulimit.close() commands.getoutput(‘ulimit -HSn 1048576’) if not isCentos7(): modify_nproc_ulimit() writeProfile() def modify_nproc_ulimit(): lines = [] if os.path.exists(NPROC_LIMIT_CONF): ulimit = open(NPROC_LIMIT_CONF, ‘r’) for line in ulimit: ma = re.match(r’.?soft ?‘, line, re.IGNORECASE) if ma: continue lines.append(line) ulimit.close() lines.append(’ soft nproc 1048576\n’) lines.append(‘root soft nproc unlimited\n’) ulimit = open(NPROC_LIMIT_CONF, ‘w’) ulimit.write(‘’.join(lines)) ulimit.close() def writeProfile(): lines = [] flag = False profile = open(PROFILE_CONF, ‘r’) for line in profile: ma = re.match(r’.?ulimit -HSn 1048576.‘, line, re.IGNORECASE) if ma is not None: flag = True lines.append(line) profile.close() if flag == False: lines.append(ULIMIT_NOFILE) profile = open(PROFILE_CONF, ‘w’) profile.write(’‘.join(lines)) profile.close() def isCentos7(): mage = commands.getoutput(‘cat /etc/redhat-release’).split(’\n’) #CentOS Linux release 7.1.1503 (Core) #CentOS release 6.6 (Final) cP = re.compile(r’.\s+(?P\d+).*', re.IGNORECASE) for mess in mage: ma = cP.match(mess) if ma is not None: ip = ma.group(‘release’) if ip == ‘7’: return True return False pass if __name__ == ‘__main__’: modify_ulimit() [/code]