關於超算中心DGX Log Report 重覆工作 [工作筆記]
事由:我大佬叫我把每月超算中心的server的使用率output出來制作一個圖表,由於這是一個重覆任務所以我想寫一個程式幫我每月自動解決這個問題
背景:
1. 超算中心的DGX使用Slurm Workload Manager去進行提交的任務:
Slurm: https://slurm.schedmd.com/documentation.html
2. 我需要先把使用率的log 存下來成為一個檔
Slurm Linux Get Report(need root):
sacct --starttime 2022-01-01 --format=User,Account,JobID,Jobname,partition,state,time,submit,start,end,elapsed,MaxRss,MaxVMSize,nnodes,ncpus,nodelist,AllocTRES%30 -p > log_output_2022_01.txt
3. 準備一個python檔把這個log output 生成一個excel and jpg, 這是進行時得出上一個月的所有log data
Linux Get DGX LOG Report Python Script (need root):
import datetime import numpy as np import pandas as pd import datetime import re # def the output report and image file name today = datetime.date.today() month_first_day = today.replace(day=1) lastMonth = month_first_day - datetime.timedelta(days=1) input_log_file_name = f"DGX_Cluster_log/dgx_log_output_{lastMonth.strftime('%Y_%m')}.txt" report_file_name = f"DGX_Cluster_log/DGX_Cluster_report_{lastMonth.strftime('%Y_%m')}" # data frame handle df = pd.read_csv(input_log_file_name, delimiter="|", header=0) df2 = df[['User', "Account", "Start", "End", "Elapsed", "AllocTRES"]].dropna() # df3 del some testing user df3 = df2[df2['User'] != 'root'] def GPU_split(tres): if "gres/gpu=" not in tres: return 0 L1 = str(tres).split("gres/gpu=") L2 = L1[-1].split(",")[0] return int(L2) df3['AllocGPU'] = df3['AllocTRES'].apply(GPU_split) def ElaspedCal(elapsed): L = re.split(r'-|:', elapsed) L2 = [int(l) for l in L] if len(L2) == 4: return L2[0]*24 + L2[1] + L2[2]/60 + L2[3]/3600 return L2[0] + L2[1]/60 + L2[2]/3600 # calc the usage and add to df3 df3['ElapsedHours'] = df3['Elapsed'].apply(ElaspedCal) df3['Usage'] = df3['ElapsedHours']*df3['AllocGPU'] #use df3 output the report result df3.to_excel(f"{report_file_name}.xlsx") # df4 use for calc the account and the usage df4 = df3[['Account','Usage']] result = df4.groupby(['Account']).sum().sort_values(by=['Usage'], ascending=False) # use df 4 get the result image and save the image log_result_image = result.plot(y='Usage', kind='bar') log_result_image.figure.savefig(f"{report_file_name}.jpg",dpi=300,bbox_inches = 'tight')
4. 生成一個sh 檔把log自動獲得再把log放到python中運行
Linux Get DGX LOG and Report Script (need root):
#!/bin/bash START_DAY=`date -d "1 month ago" +"%Y-%m-01"` END_DAY=`date -d "today" +"%Y-%m-01"` FILE_NAME=`date -d "1 month ago" +"%Y_%m"` echo "Generate DGX Log file start:${START_DAY} end:${END_DAY}" echo "-------------------------------------------------------" echo "sacct --starttime $START_DAY --endtime $END_DAY --format=User,Account,JobID,Jobname,partition,state,time,submit,start,end,elapsed,MaxRss,MaxVMSize,nnodes,ncpus,nodelist,AllocTRES%30 -p > DGX_Cluster_log/dgx_log_output_$FILE_NAME.txt" sudo sacct --starttime $START_DAY --endtime $END_DAY --format=User,Account,JobID,Jobname,partition,state,time,submit,start,end,elapsed,MaxRss,MaxVMSize,nnodes,ncpus,nodelist,AllocTRES%30 -p > DGX_Cluster_log/dgx_log_output_$FILE_NAME.txt chown raywong:raywong DGX_Cluster_log/dgx_log_output_$FILE_NAME.txt echo "--------------------" echo "Done ~ DGX Log Output~" echo "-----------------------------------" echo "Generate DGX Usage Report Output" source /home/raywong/.bashrc source activate rayenv python3 generate_dgx_report.py echo "------------------------------" echo "Done ~ DGX Usage Report Output"
最後再懶一點就把這個script 加到crontab中運行,每一個月運行一次
需要使用 crontab的簡單教學:
執行 crontab 命令,如果輸出 command not found,就表明沒有安裝這是要先安裝crontab,網上有教程,這裡不再贅述我的Linux服務器系統為Centos7,crontab 已經安裝好_執行命令:
crontab -e
#Crontab 格式 分 时 日 月 周 执行命令 第 1 列分钟 1~59,每分钟用 *或者*/1表示,整点分钟数为00或0 第 2 列小时 1~23(0 表示 0 点) 第 3 列日 1~31 第 4 列月 1~12 第 5 列星期 0~6(0 表示星期天) 第 6 列要运行的命令 0 3 * * * /get_log.sh,此命令表示在每天的凌晨三点执行一次脚本,可自行调整时间 这样定期数据备份就完成了
最後更懶就連發給用戶的email都自動定時發出
最後這樣我就解決了一個每月的重覆工作了!哈哈~
Comments
Post a Comment