“Python和运维”的版本间的差异
跳到导航
跳到搜索
(未显示同一用户的17个中间版本) | |||
第1行: | 第1行: | ||
=python常用的运维脚本= | =python常用的运维脚本= | ||
+ | == Python 3 面向对象来监控 Linux 上 Nginx 进程的脚本== | ||
+ | <pre> | ||
+ | import psutil | ||
+ | |||
+ | class NginxMonitor: | ||
+ | def __init__(self): | ||
+ | self.nginx_processes = [] | ||
+ | |||
+ | def find_nginx_processes(self): | ||
+ | for proc in psutil.process_iter(): | ||
+ | try: | ||
+ | if 'nginx' in proc.name(): | ||
+ | self.nginx_processes.append(proc) | ||
+ | except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | ||
+ | continue | ||
+ | return self.nginx_processes | ||
+ | |||
+ | def print_nginx_process_info(self): | ||
+ | processes = self.find_nginx_processes() | ||
+ | if not processes: | ||
+ | print("No nginx process found") | ||
+ | else: | ||
+ | for proc in processes: | ||
+ | print("Nginx Process ID: {}".format(proc.pid)) | ||
+ | print("Parent Process ID: {}".format(proc.ppid)) | ||
+ | print("CPU Percent: {}%".format(proc.cpu_percent(interval=1))) | ||
+ | print("Memory Usage: {} bytes".format(proc.memory_info().rss)) | ||
+ | print("Status: {}".format(proc.status())) | ||
+ | print("Command Line: {}".format(proc.cmdline())) | ||
+ | |||
+ | |||
+ | if __name__ == '__main__': | ||
+ | monitor = NginxMonitor() | ||
+ | monitor.print_nginx_process_info() | ||
+ | |||
+ | # ➜ tmp py3 ngmonitor.py | ||
+ | # Nginx Process ID: 70189 | ||
+ | # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70189, name='nginx', status='sleeping', started='15:14:38')> | ||
+ | # CPU Percent: 0.0% | ||
+ | # Memory Usage: 1777664 bytes | ||
+ | # Status: sleeping | ||
+ | # Command Line: ['nginx:', 'master', 'process', '/usr/sbin/nginx', '-g', 'daemon', 'on;', 'master_process', 'on;'] | ||
+ | # Nginx Process ID: 70190 | ||
+ | # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70190, name='nginx', status='sleeping', started='15:14:38')> | ||
+ | # CPU Percent: 0.0% | ||
+ | # Memory Usage: 5734400 bytes | ||
+ | # Status: sleeping | ||
+ | # Command Line: ['nginx: worker process', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] | ||
+ | # Nginx Process ID: 70191 | ||
+ | # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70191, name='nginx', status='sleeping', started='15:14:38')> | ||
+ | # CPU Percent: 0.0% | ||
+ | # Memory Usage: 5734400 bytes | ||
+ | # Status: sleeping | ||
+ | # Command Line: ['nginx: worker process', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] | ||
+ | |||
+ | ai说明还真的 6 | ||
+ | |||
+ | 1. 类的定义 | ||
+ | |||
+ | NginxMonitor类: | ||
+ | __init__方法初始化一个空列表nginx_processes,用于存储找到的 Nginx 进程。 | ||
+ | |||
+ | |||
+ | 2. 方法 | ||
+ | |||
+ | find_nginx_processes方法: | ||
+ | 通过遍历系统中的所有进程(使用psutil.process_iter())。 | ||
+ | 对于每个进程,尝试获取其名称,如果名称中包含nginx,则将该进程添加到nginx_processes列表中。 | ||
+ | 在这个过程中,捕获可能出现的进程不存在、访问被拒绝和僵尸进程等异常情况。 | ||
+ | print_nginx_process_info方法: | ||
+ | 首先调用find_nginx_processes方法获取 Nginx 进程列表。 | ||
+ | 如果列表为空,打印提示信息表示没有找到 Nginx 进程。 | ||
+ | 如果列表不为空,对于每个 Nginx 进程,打印出进程 ID(pid)、父进程 ID(ppid)、CPU 使用率(cpu_percent)、内存使用量(memory_info().rss,这里的rss表示常驻内存集大小)、进程状态(status)和命令行参数(cmdline)。 | ||
+ | |||
+ | |||
+ | 3. 主程序 | ||
+ | |||
+ | 在if __name__ == "__main__"部分,实例化NginxMonitor类,并调用print_nginx_process_info方法来打印 Nginx 进程的相关信息。 | ||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看 == | ||
+ | <pre> | ||
+ | |||
+ | |||
+ | |||
+ | 数据处理与脚本 之 Python 脚本,从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看。 | ||
+ | |||
+ | |||
+ | # errmv.py | ||
+ | def extract_errors(log_file,output_file): | ||
+ | with open(log_file,'r') as infile, open(output_file,'w') as outfile: | ||
+ | for line in infile: | ||
+ | # //if "ERROR" in line: | ||
+ | if "failed" in line: | ||
+ | outfile.write(line) | ||
+ | |||
+ | extract_errors('nglog.log','error.log') | ||
+ | |||
+ | |||
+ | ➜ tmp cat nglog.log | ||
+ | 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 | ||
+ | 2024/09/06 17:44:59 [notice] 65501#65501: using inherited sockets from "" | ||
+ | |||
+ | tmp py3 errmv.py | ||
+ | |||
+ | |||
+ | ➜ tmp cat error.log | ||
+ | 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | == py3 批量修改文件后缀名 或者去掉后缀名== | ||
+ | |||
+ | <pre> | ||
+ | #!/usr/bin/python3 | ||
+ | # -*- coding:utf-8 -*- | ||
+ | |||
+ | # python批量更换后缀名 注意 需要把脚本放在与修改的文件同一目录下,因为filenames只是获取了程序目录的文件名。 | ||
+ | import os | ||
+ | |||
+ | # 列出当前目录下所有的文件 | ||
+ | files = os.listdir('.') | ||
+ | for filename in files: | ||
+ | portion = os.path.splitext(filename) | ||
+ | # 如果后缀是.dat | ||
+ | if portion[1] == ".txt": | ||
+ | # 这里是把 .txt去掉 重新组合文件名和后缀名 | ||
+ | newname = portion[0] | ||
+ | |||
+ | #txt 文件改为sh 要用这个记得打开下行注释 | ||
+ | #newname = portion[0] + ".sh" | ||
+ | |||
+ | #os.rename(filename,newname) | ||
+ | os.replace(filename,newname) | ||
+ | |||
+ | # As of Python version 3.3 and later, it is generally preferred to use os.replace instead of os.rename so FileExistsError is not raised if the destination file already exists. | ||
+ | |||
+ | |||
+ | |||
+ | #!/usr/bin/python3 | ||
+ | # -*- coding:utf-8 -*- | ||
+ | # python批量更换后缀名 进入目录的版本 利用os.chdir | ||
+ | import os | ||
+ | import sys | ||
+ | os.chdir(r'~/tmp') | ||
+ | |||
+ | # 列出当前目录下所有的文件 | ||
+ | files = os.listdir('./') | ||
+ | print('files',files) | ||
+ | |||
+ | for filename in files: | ||
+ | portion = os.path.splitext(filename) | ||
+ | # 如果后缀是.txt | ||
+ | if portion[1] == ".txt": | ||
+ | # 这里是把 .txt去掉 重新组合文件名和后缀名 | ||
+ | newname = portion[0] | ||
+ | |||
+ | #txt 文件改为sh 要用这个记得打开下行注释 | ||
+ | #newname = portion[0] + ".sh" | ||
+ | |||
+ | os.rename(filename,newname) | ||
+ | #os.rename(filename,newname) | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==python批量更换后缀名 会返回脚本所在目录的版本 == | ||
+ | <pre> | ||
+ | #!/usr/bin/python3 | ||
+ | # -*- coding:utf-8 -*- | ||
+ | # python批量更换后缀名 会返回脚本所在目录的版本 | ||
+ | import os | ||
+ | import sys | ||
+ | |||
+ | # Set the path | ||
+ | path ='~' | ||
+ | # save current working directory | ||
+ | saved_cwd=os.getcwd() | ||
+ | # change your cwd to the directory which contains files | ||
+ | os.chdir(path) | ||
+ | |||
+ | # 列出当前目录下所有的文件 | ||
+ | files = os.listdir('./') | ||
+ | print('files',files) | ||
+ | |||
+ | for filename in files: | ||
+ | portion = os.path.splitext(filename) | ||
+ | # 如果后缀是.txt | ||
+ | if portion[1] == ".txt": | ||
+ | # 这里是把 .txt去掉 重新组合文件名和后缀名 | ||
+ | newname = portion[0] | ||
+ | |||
+ | #txt 文件改为sh 要用这个记得打开下行注释 | ||
+ | #newname = portion[0] + ".sh" | ||
+ | |||
+ | os.rename(filename,newname) | ||
+ | #os.rename(filename,newname) | ||
+ | |||
+ | os.chdir(saved_cwd) | ||
+ | |||
+ | # https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python#2491232 | ||
+ | |||
+ | </pre> | ||
+ | [https://blog.csdn.net/rosefun96/article/details/78968490 python文件操作3--批量修改文件后缀名] | ||
+ | |||
+ | |||
[https://github.com/fupinglee/MyPython github.com 一些常用的Python脚本] | [https://github.com/fupinglee/MyPython github.com 一些常用的Python脚本] | ||
第6行: | 第216行: | ||
[https://www.yisu.com/zixun/7532.html python常用的运维脚本有哪些] | [https://www.yisu.com/zixun/7532.html python常用的运维脚本有哪些] | ||
+ | |||
+ | [https://www.zhihu.com/question/28661987 zhihu 你用 Python 写过哪些有趣的脚本?] | ||
+ | |||
+ | [https://zhuanlan.zhihu.com/p/30544304 python爬取廖雪峰教程存为PDF] | ||
+ | |||
+ | =os.path.splitext= | ||
+ | |||
+ | os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作 | ||
+ | |||
+ | https://www.cnblogs.com/rychh/articles/9745932.html | ||
+ | |||
+ | https://blog.csdn.net/u011509971/article/details/70244688 | ||
+ | |||
+ | =R= | ||
+ | [https://blog.csdn.net/qq_32311359/article/details/86611381 python 遍历文件夹下所有文件名 并去掉后缀存入txt] | ||
+ | |||
+ | [[category:python]] |
2024年10月27日 (日) 08:28的最新版本
目录
python常用的运维脚本
Python 3 面向对象来监控 Linux 上 Nginx 进程的脚本
import psutil class NginxMonitor: def __init__(self): self.nginx_processes = [] def find_nginx_processes(self): for proc in psutil.process_iter(): try: if 'nginx' in proc.name(): self.nginx_processes.append(proc) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): continue return self.nginx_processes def print_nginx_process_info(self): processes = self.find_nginx_processes() if not processes: print("No nginx process found") else: for proc in processes: print("Nginx Process ID: {}".format(proc.pid)) print("Parent Process ID: {}".format(proc.ppid)) print("CPU Percent: {}%".format(proc.cpu_percent(interval=1))) print("Memory Usage: {} bytes".format(proc.memory_info().rss)) print("Status: {}".format(proc.status())) print("Command Line: {}".format(proc.cmdline())) if __name__ == '__main__': monitor = NginxMonitor() monitor.print_nginx_process_info() # ➜ tmp py3 ngmonitor.py # Nginx Process ID: 70189 # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70189, name='nginx', status='sleeping', started='15:14:38')> # CPU Percent: 0.0% # Memory Usage: 1777664 bytes # Status: sleeping # Command Line: ['nginx:', 'master', 'process', '/usr/sbin/nginx', '-g', 'daemon', 'on;', 'master_process', 'on;'] # Nginx Process ID: 70190 # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70190, name='nginx', status='sleeping', started='15:14:38')> # CPU Percent: 0.0% # Memory Usage: 5734400 bytes # Status: sleeping # Command Line: ['nginx: worker process', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] # Nginx Process ID: 70191 # Parent Process ID: <bound method Process.ppid of psutil.Process(pid=70191, name='nginx', status='sleeping', started='15:14:38')> # CPU Percent: 0.0% # Memory Usage: 5734400 bytes # Status: sleeping # Command Line: ['nginx: worker process', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] ai说明还真的 6 1. 类的定义 NginxMonitor类: __init__方法初始化一个空列表nginx_processes,用于存储找到的 Nginx 进程。 2. 方法 find_nginx_processes方法: 通过遍历系统中的所有进程(使用psutil.process_iter())。 对于每个进程,尝试获取其名称,如果名称中包含nginx,则将该进程添加到nginx_processes列表中。 在这个过程中,捕获可能出现的进程不存在、访问被拒绝和僵尸进程等异常情况。 print_nginx_process_info方法: 首先调用find_nginx_processes方法获取 Nginx 进程列表。 如果列表为空,打印提示信息表示没有找到 Nginx 进程。 如果列表不为空,对于每个 Nginx 进程,打印出进程 ID(pid)、父进程 ID(ppid)、CPU 使用率(cpu_percent)、内存使用量(memory_info().rss,这里的rss表示常驻内存集大小)、进程状态(status)和命令行参数(cmdline)。 3. 主程序 在if __name__ == "__main__"部分,实例化NginxMonitor类,并调用print_nginx_process_info方法来打印 Nginx 进程的相关信息。
从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看
数据处理与脚本 之 Python 脚本,从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看。 # errmv.py def extract_errors(log_file,output_file): with open(log_file,'r') as infile, open(output_file,'w') as outfile: for line in infile: # //if "ERROR" in line: if "failed" in line: outfile.write(line) extract_errors('nglog.log','error.log') ➜ tmp cat nglog.log 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 2024/09/06 17:44:59 [notice] 65501#65501: using inherited sockets from "" tmp py3 errmv.py ➜ tmp cat error.log 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61
py3 批量修改文件后缀名 或者去掉后缀名
#!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 注意 需要把脚本放在与修改的文件同一目录下,因为filenames只是获取了程序目录的文件名。 import os # 列出当前目录下所有的文件 files = os.listdir('.') for filename in files: portion = os.path.splitext(filename) # 如果后缀是.dat if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" #os.rename(filename,newname) os.replace(filename,newname) # As of Python version 3.3 and later, it is generally preferred to use os.replace instead of os.rename so FileExistsError is not raised if the destination file already exists. #!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 进入目录的版本 利用os.chdir import os import sys os.chdir(r'~/tmp') # 列出当前目录下所有的文件 files = os.listdir('./') print('files',files) for filename in files: portion = os.path.splitext(filename) # 如果后缀是.txt if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" os.rename(filename,newname) #os.rename(filename,newname)
python批量更换后缀名 会返回脚本所在目录的版本
#!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 会返回脚本所在目录的版本 import os import sys # Set the path path ='~' # save current working directory saved_cwd=os.getcwd() # change your cwd to the directory which contains files os.chdir(path) # 列出当前目录下所有的文件 files = os.listdir('./') print('files',files) for filename in files: portion = os.path.splitext(filename) # 如果后缀是.txt if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" os.rename(filename,newname) #os.rename(filename,newname) os.chdir(saved_cwd) # https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python#2491232
os.path.splitext
os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
https://www.cnblogs.com/rychh/articles/9745932.html
https://blog.csdn.net/u011509971/article/details/70244688