diff --git a/tools/vmcore/Makefile b/tools/vmcore/Makefile deleted file mode 100644 index df2d5a34aa7387d73d5c183cf7942ede8b5ca03c..0000000000000000000000000000000000000000 --- a/tools/vmcore/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -CFLAGS=-Wall -g -O3 -Ithird_party/libnfs/include/nfsc -Ithird_party/libnfs/include -Ithird_party/libnfs/mount -LIBS=-lpthread -NAME=vmcore_collect -NFS_LIB=third_party/libnfs.a -VER=$(shell git log --pretty=format:%h -1) -DT=$(shell date '+%Y%m%d') -DEF=-DCOMMIT_ID="\"$(VER)|$(DT)\"" -all: - make -C third_party - gcc $(CFLAGS) $(DEF) -o $(NAME) $(NAME).c $(NFS_LIB) ${LIBS} - -clean: - rm -f $(NAME) - make -C third_party clean diff --git a/tools/vmcore/init_server.sh b/tools/vmcore/init_server.sh deleted file mode 100644 index f9209823b79287ada20756fc864cf939d6075b12..0000000000000000000000000000000000000000 --- a/tools/vmcore/init_server.sh +++ /dev/null @@ -1,17 +0,0 @@ -#! /bin/sh -yum install nfs-utils rpcbind -y -systemctl start rpcbind && systemctl enable rpcbind -systemctl start nfs && systemctl enable nfs - -if [ "$#" -ge 2 ]; then - file_path=$(readlink -f $1) - mkdir $file_path - echo "$file_path $2(rw,async)" >> /etc/exports - exportfs -rv - chmod -R 777 $file_path -else - mkdir /usr/vmcore-nfs - echo "/usr/vmcore-nfs 172.16.139.0/24(rw,async)" >> /etc/exports - exportfs -rv - chmod -R 777 /usr/vmcore-nfs -fi diff --git a/tools/vmcore/init_vmcore.sh b/tools/vmcore/init_vmcore.sh deleted file mode 100644 index 89f6674f026a460e73dd4897d267889fec9b997c..0000000000000000000000000000000000000000 --- a/tools/vmcore/init_vmcore.sh +++ /dev/null @@ -1,13 +0,0 @@ -#! /bin/sh -yum install nfs-utils rpcbind -y -systemctl start rpcbind && systemctl enable rpcbind -systemctl start nfs && systemctl enable nfs - -make -cp vmcore_collect /usr/bin -cp vmcore_collect.conf.example /etc/vmcore_collect.conf -cp vmcore-collect.service /usr/lib/systemd/system/vmcore-collect.service -chmod 644 /usr/lib/systemd/system/vmcore-collect.service -systemctl daemon-reload -systemctl enable vmcore-colect.service -systemctl start vmcore-collect.service diff --git a/tools/vmcore/parse_panic.py b/tools/vmcore/parse_panic.py deleted file mode 100644 index 883d1feb52f49cf22a182ddb7d7aedac3fb9ba1b..0000000000000000000000000000000000000000 --- a/tools/vmcore/parse_panic.py +++ /dev/null @@ -1,499 +0,0 @@ -# -*- coding: utf-8 -*- -# @Author: lichen/zhilan - -import os -import sys -import time -import subprocess -import re -import sqlite3 -import json -import traceback -import importlib -import argparse -import requests -import vmcore_const -import time -from datetime import datetime -import threading -import queue -queue = queue.Queue() - -if sys.version[0] == '2': - reload(sys) - sys.setdefaultencoding('utf8') - -# crashkey_type={ -# 0:func_name -# 1:calltrace -# 2:crashkey -# 3:bugon_file -#} -nfs_root = '/usr/vmcore-nfs' -ltime_pattern = re.compile(r'^\[\s*([0-9]+)\..*\]') -rip_pattern = re.compile(r'\[\s*\S+\] RIP: 0010:.*\[<([0-9a-f]+)>\] (.+)') -rip_pattern_1 = re.compile(r'\[\s*\S+\] RIP: 0010:(\S+)') -rip_pattern_2 = re.compile(r'\[\s*\S+\] RIP .*\[<([0-9a-f]+)>\] (.+)') -ripmod_pattern = re.compile(r'\[\s*\S+\] RIP.* \[(\S+)\]$') -bugat_pattern = re.compile(r'.+\] kernel BUG at (\S+)!') -ver_pattern = re.compile(r'Comm: (\S*).*(Tainted:|Not tainted).* (\S+) #') -unload_pattern = re.compile(r'\[last unloaded: (\S+)\]') -title_pattern = re.compile(r'\[\s*\S+\] ((BUG:|Kernel panic|Bad pagetable:|divide error:|kernel BUG at|general protection fault:) .+)') -vertype_pattern = re.compile(r'(\d+)\.(\d+)\.') -last_strhost = '' - -ignore_funcs = ["schedule","schedule_timeout","ret_from_fork","kthread", - "do_syscall_64","entry_SYSCALL_64_after_swapgs","system_call_fastpath","fastpath", - "entry_SYSCALL_64_after_hwframe", - "page_fault","do_page_fault","_do_page_fault","worker_thread", - "start_secondary","cpu_startup_entry","arch_cpu_idle","default_idle", - "do_IRQ","common_interrupt","irq_exit","do_softirq", - "__schedule","io_schedule_timeout","io_schedule","dump_stack", - "exit_to_usermode_loop","stub_clone","schedule_preempt_disabled","oom_kill_process", - "unwind_backtrace","dump_header","show_stack","dump_backtrace","panic","watchdog_timer_fn", - "nmi_panic","watchdog_overflow_callback","__perf_event_overflow","perf_event_overflow","intel_pmu_handle_irq", - "perf_event_nmi_handler","nmi_handle","do_nmi","end_repeat_nmi","watchdog", - "__hrtimer_run_queues","hrtimer_interrupt","local_apic_timer_interrupt","smp_apic_timer_interrupt","apic_timer_interrupt", - "__pv_queued_spin_lock_slowpath","queued_spin_lock_slowpath" -] - -def get_column_value(column, line): - match = rip_pattern.match(line) - if match is None: - match = rip_pattern_2.match(line) - - if column['func_name']=='NA' and match: - column['rip']=match.group(1) - column['func_name']=match.group(2).split('+')[0] - column['func_name']=column['func_name'].split('.')[0] - ripmod_match = ripmod_pattern.match(line.strip()) - if ripmod_match: - column['ripmod']=ripmod_match.group(3) - match = rip_pattern_1.match(line) - if column['func_name']=='NA' and column.get('func_name_1','') =='NA' and match: - column['func_name_1']=match.group(1).split('+')[0] - column['func_name_1']=column['func_name_1'].split('.')[0] - - match = bugat_pattern.match(line) - if match: - column['bugat'] = match.group(1) - idx = line.find('Comm:') - if idx > 0: - match = ver_pattern.match(line, idx) - if match: - column['comm'] = match.group(1) - column['ver'] = match.group(3) - idx = line.find('[last unloaded:') - if idx > 0: - match = unload_pattern.match(line, idx) - if match: - column['unload'] = match.group(1) - match = title_pattern.match(line) - if match and column['title'] == 'NA': - column['title'] = match.group(1) - if column['func_name'] != 'NA': - column['tmp_func_name'] = column['func_name'] - column['tmp_rip'] = column['rip'] - column['tmp_ripmod'] = column['ripmod'] - column['func_name'] = '' - column['rip'] = '' - column['ripmod'] = '' - -def get_stamp(line): - match = ltime_pattern.match(line) - if match: - return int(match.group(1)) - return 0 - -def get_last_time(f): - ret = 10 - try: - f.seek(-512, os.SEEK_END) - except: - pass - for line in f.readlines(): - ret = get_stamp(line) - if ret > 0: - break - f.seek(0, os.SEEK_SET) - return ret-10 - -def fix_func_name(column): - if column['dmesg'].find('SysRq : Trigger a crash') > 0: - column['func_name'] = 'sysrq_handle_crash' - column['title'] = 'sysrq: SysRq : Trigger a crash' - column['status'] = vmcore_const.STATUS_SYSRQ - column['crashkey_type'] = 2 - column['crashkey'] = 'sysrq_handle_crash' - if column['dmesg'].find('Kernel panic - not syncing: Fatal machine check') > 0: - column['func_name'] = 'fatal_machine_check' - column['title'] = 'Kernel panic - not syncing: Fatal machine check' - column['status'] = vmcore_const.STATUS_HWERROR - column['crashkey_type'] = 2 - column['crashkey'] = 'fatal_machine_check' - column['panic_class'] = 'HardwareError' - if column['dmesg'].find('Kernel panic - not syncing: Fatal hardware error') > 0: - column['func_name'] = 'fatal_hardware_error' - column['title'] = 'Kernel panic - not syncing: Fatal machine check' - column['status'] = vmcore_const.STATUS_HWERROR - column['crashkey_type'] = 2 - column['crashkey'] = 'fatal_hardware_error' - column['panic_class'] = 'HardwareError' - if column['dmesg'].find('Fatal local machine check') > 0: - column['func_name'] = 'fatal_machine_check' - column['title'] = 'Kernel panic - not syncing: Fatal local machine check' - column['status'] = vmcore_const.STATUS_HWERROR - column['crashkey_type'] = 2 - column['crashkey'] = 'fatal_machine_check' - column['panic_class'] = 'HardwareError' - if 'bugat' in column: - column['bugon_file'] = column['bugat'].split(':')[0] - column['crashkey_type'] = 3 - -def parse_file(name, column): - f = open(name, 'r') - result = '' - for line in f.readlines(): - if line.find('Modules linked in') >= 0: - column['modules'] = line[line.find(':')+1:] - if len(column['modules']) >= 512: - column['modules'] = column['modules'][:-512] - result += line - get_column_value(column, line) - f.close() - column['dmesg'] = result - column['dmesg_file'] = name - if 'tmp_func_name' in column and column['func_name'] == 'NA' and column['tmp_func_name'] != 'NA': - column['func_name'] = column['tmp_func_name'] - column['rip'] = column['tmp_rip'] - column['ripmod'] = column['ripmod'] - fix_func_name(column) - if column['ripmod'] != 'NA': - if column['ripmod'] not in vmcore_const.BASEMODS: - column['panic_class'] = 'Module(%s)'%(column['ripmod']) - -line_pattern = re.compile(r'.+[0-9]+\].+\[.*\][? ]* (\S+)\+0x(\S+)/0x(\S+)') -line_pattern_1 = re.compile(r'.+[0-9]+\][? ]*(\S+)\+0x(\S+)/0x(\S+)') -def get_calltrace(column): - meettitle = 0 - list1 = [] - lines = column['dmesg'].split('\n') - modname = [] - tmplist = [] - workqueue = '' - nocalltrace = True - hung_flag = False - if column['title'].find('unrecovered softlockup') >= 0: - hung_flag = True - - invalidrip = False - if (column['rip'] == 'NA'and column['func_name'] == 'NA') or column['func_name'].startswith('0x'): - invalidrip = True - - badrip = False - if column['dmesg'].find('Code: Bad RIP value.') >= 0: - badrip = True - - question_continue = True - question_count = 0 - - for r in lines: - if r.find(column['title']) >= 0: - nocalltrace = True - meettitle = 1 - tmplist.extend(list1) - del list1[:] - question_count = 0 - question_continue = True - continue - - if r.find('Workqueue: events ') >= 0: - idx = r.find('Workqueue: events ') - workqueue = r[idx+18:] - - if r.find('EFLAGS: ') >= 0: - idx = r.find('EFLAGS: ') - eflags = r[idx+8:] - #print 'eflags',eflags - try: - eflags = int(eflags,16) - if (eflags >> 9) % 2 == 0: - badrip = True - except: - pass - if r.find("<>") >= 0: - if column['func_name'] == 'NA': - tmpline = lines[lines.index(r)-1] - m = line_pattern.match(tmpline) - if m: - column['func_name'] = m.group(1) - else: - m = line_pattern_1.match(tmpline) - if m: - column['func_name'] = m.group(1) - - if r.find('') >= 0: - badrip = True - - if hung_flag and r.find('') >= 0: - try: - if r.find('> ') >= 0 and r.find(' <') >= 0: - idx = r.find(' <') - idx2 = r.rfind('> ',0) - r = r[0:idx] + r[idx2+1:] - except: - import traceback - traceback.print_exc() - del list1[:] - question_count = 0 - question_continue = True - - if r.find("Call Trace:") > 0 or r.find("<>") > 0 or r.find("") > 0 or r.find("") >= 0: - try: - if r.find('> ') >= 0 and r.find(' <') >= 0: - idx = r.find(' <') - idx2 = r.rfind('> ',0) - r = r[0:idx] + r[idx2+1:] - except: - import traceback - traceback.print_exc() - del list1[:] - question_count = 0 - question_continue = True - modname = [] - - if r.find('?') >= 0: - if workqueue != '' and r.find(workqueue) >= 0: - list1.append(workqueue) - #print r - #print invalidrip,badrip,question_continue - if invalidrip and badrip and question_continue: - m2 = line_pattern.match(r) - if m2: - #print m2.group(1),m2.group(2),m2.group(3) - if m2.group(1).split('.')[0] == column['func_name'] or m2.group(1) in ignore_funcs: - continue - nocalltrace = False - if m2.group(2) != m2.group(3): - tmp = m2.group(1) - tmp = tmp.split('.')[0] - list1.append(tmp) - #print 'append: ',m2.group(1) - #print list1 - question_count += 1 - else: - m2 = line_pattern_1.match(r) - if m2: - #print m2.group(1),m2.group(2),m2.group(3) - if m2.group(1).split('.')[0] == column['func_name'] or m2.group(1) in ignore_funcs: - continue - nocalltrace = False - if m2.group(2) != m2.group(3): - tmp = m2.group(1) - tmp = tmp.split('.')[0] - list1.append(tmp) - #print 'append: ',m2.group(1) - #print list1 - question_count += 1 - continue - if question_count > 0: - question_continue = False - - m = line_pattern.match(r) - if m: - nocalltrace = False - if m.group(1).split('.')[0] == column['func_name'] or m.group(1) in ignore_funcs: - continue - if m.group(1) == 'panic': - del list1[:] - question_count = 0 - question_continue = True - modname = [] - continue - if len(list1) == 0 and m.group(1) in ignore_funcs: - continue - if len(modname) < 2: - modname.append(r.strip()) - tmp = m.group(1) - tmp = tmp.split('.')[0] - list1.append(tmp) - #print 'append: ',m.group(1) - #print list1 - else: - m = line_pattern_1.match(r) - if m: - nocalltrace = False - if m.group(1).split('.')[0] == column['func_name'] or m.group(1) in ignore_funcs: - continue - if m.group(1) == 'panic': - del list1[:] - question_count = 0 - question_continue = True - modname = [] - continue - if len(list1) == 0 and m.group(1) in ignore_funcs: - continue - if len(modname) < 2: - modname.append(r.strip()) - tmp = m.group(1) - tmp = tmp.split('.')[0] - list1.append(tmp) - #print 'append: ',m.group(1) - #print list1 - else: - if len(list1) > 0 and meettitle == 1: - break - if len(list1) == 0 and nocalltrace: - list1 = tmplist - - if column['func_name'] == 'NA' and len(list1) > 0: - column['func_name'] = list1[0] - del list1[0] - - calltrace = column['func_name'] - if calltrace != '': - calltrace = calltrace.split('+')[0] - if len(list1) > 2: - list1 = list1[0:2] - for i in list1: - calltrace = ''.join([calltrace,'$',i]) - column['calltrace'] = calltrace - - -def clarify_panic_type(column): - column['panic_type'] = 0 - if column['title'].find('divide error') >= 0: - column['panic_type'] = vmcore_const.PANIC_DIVIDEERROR - elif column['bugon_file'] != 'NA': - column['panic_type'] = vmcore_const.PANIC_BUGON - elif column['title'].find('NULL pointer dereference') >= 0: - column['panic_type'] = vmcore_const.PANIC_NULLPOINTER - elif column['title'].find('Kernel stack is corrupted') >= 0: - column['panic_type'] = vmcore_const.PANIC_STACKCORRUPTION - elif column['title'].find('hard LOCKUP') >= 0: - column['panic_type'] = vmcore_const.PANIC_HARDLOCKUP - elif column['title'].find('hung_task') >= 0: - column['panic_type'] = vmcore_const.PANIC_HUNGTASK - elif column['title'].find('RCU Stall') >= 0: - column['panic_type'] = vmcore_const.PANIC_RCUSTALL - elif (column['title'].find('soft lockup') >= 0 or column['title'].find('softlockup') >= 0): - column['panic_type'] = vmcore_const.PANIC_SOFTLOCKUP - -def check_panic(column): - if 'rawdmesg' not in column and os.path.isfile(column['dmesg_file']) == False: - return False - - matched = False - if 'rawdmesg' in column: - parse_rawdmesg(column) - else: - parse_file(column['dmesg_file'], column) - - m = vertype_pattern.match(column['ver']) - if m: - column['vertype'] = int(m.group(1)) * 100 + int(m.group(2)) - - get_calltrace(column) - if column['calltrace'] == 'NA': - column['crashkey_type'] = 0 - if column['crashkey_type'] == 0 and column['func_name'] != 'NA': - column['crashkey'] = '%d$%s'%(column['vertype'],column['func_name']) - elif column['crashkey_type'] == 1 and column['calltrace'] != 'NA': - column['crashkey'] = '%d$%s'%(column['vertype'],column['calltrace']) - elif column['crashkey_type'] == 2 and column['crashkey'] != 'NA': - column['crashkey'] = '%d$%s'%(column['vertype'],column['crashkey']) - elif column['crashkey_type'] == 3 and column['bugon_file'] != 'NA': - column['crashkey'] = '%d$%s$%s'%(column['vertype'],column['bugon_file'],column['calltrace']) - - clarify_panic_type(column) - #return False - - ip={'ip':column['ip']} - host_url = "http://127.0.0.1:8000/api/v1/host/" - res = requests.get(host_url,params=ip) - if res.status_code != 200 or res.text == '[]': - print("查询主机名失败") - return False - - column['hostname'] = res.json()['data'][0]['hostname'] - vmcore_url = "http://127.0.0.1:8000/api/v1/vmcore/" - data = json.dumps(column) - headers = {'content-type': 'application/json'} - res = requests.post(url=vmcore_url, data=data, headers=headers) - print(res.json()) - if res.status_code == 200: - print(f"add {column['name']} to db") - return True - else: - print("插入失败") - return False - -def do_cmd(cmd): - output = os.popen(cmd) - ret = output.read().strip() - output.close() - return ret - -def init_column(column): - column['upload_time'] = int(time.time()) - column['vmcore_file'] = 'NA' - column['dmesg_file'] = 'NA' - column['rip'] = 'NA' - column['ripmod'] = 'NA' - column['comm'] = 'NA' - column['ver'] = 'NA' - column['vertype'] = 0 - column['func_name'] = 'NA' - column['title'] = 'NA' - column['status'] = 0 - column['calltrace'] = 'NA' - column['bugon_file'] = '' - column['crashkey_type'] = 1 - column['crashkey'] = 'NA' - column['modules'] = 'NA' - column['panic_type'] = 0 - column['panic_class'] = 'BaseKernel' - column['issue_id'] = 0 - -def parse_new_crash(crash_dir): - try: - column = {} - column['name'] = crash_dir.split('/')[-1] - core_time = column['name'].split('_')[0] - core_time = datetime.strptime(core_time, "%Y%m%d%H%M%S") - column['core_time'] = core_time.strftime("%Y-%m-%d %H:%M:%S") - column['ip'] = column['name'].split('_')[1] - column['hostname'] = column['name'].split('_')[1] - init_column(column) - column['dmesg_file'] = '%s/vmcore-dmesg.txt' % crash_dir - ret = check_panic(column) - if ret: - with open('%s/.upload'%crash_dir,'w'): - pass - except: - import traceback - traceback.print_exc() - -def main(): - global nfs_root - if len(sys.argv) > 1: - nfs_root = sys.argv[1] - dirs_list = [] - #while True: - files = os.listdir(nfs_root) - files_path = [f'{nfs_root}/{file}' for file in files] - for file in files_path: - if os.path.isfile(file): - continue - dirs_list.append(file) - dirs_list.sort(key=lambda fp: os.path.getmtime(fp),reverse=True) - for dir in dirs_list: - tmp = '%s/.upload' % dir - if os.path.exists(tmp): - break - parse_new_crash(dir) - #time.sleep(20) - - -if __name__ == "__main__": - main() diff --git a/tools/vmcore/third_party/libnfs.tar b/tools/vmcore/third_party/libnfs.tar deleted file mode 100644 index 8f3767198b67988c33e658f5bc848f5549f9e65e..0000000000000000000000000000000000000000 Binary files a/tools/vmcore/third_party/libnfs.tar and /dev/null differ diff --git a/tools/vmcore/vmcore-collect.service b/tools/vmcore/vmcore-collect.service deleted file mode 100644 index cb2bda31976c1b8c8f246c8766b677c68e795f33..0000000000000000000000000000000000000000 --- a/tools/vmcore/vmcore-collect.service +++ /dev/null @@ -1,14 +0,0 @@ -[Unit] -Description=Collect vmcore file to oss -After=network.target network-online.target remote-fs.target basic.target -DefaultDependencies=no - -[Service] -Type=forking -ExecStart=/usr/bin/vmcore_collect -StartLimitInterval=0 -StandardOutput=syslog -StandardError=inherit - -[Install] -WantedBy=multi-user.target diff --git a/tools/vmcore/vmcore_collect.c b/tools/vmcore/vmcore_collect.c deleted file mode 100644 index 1b39ae0d31bc3cb77ce1a0cf0f434c40f2515062..0000000000000000000000000000000000000000 --- a/tools/vmcore/vmcore_collect.c +++ /dev/null @@ -1,647 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "libnfs.h" -#include "libnfs-raw.h" -#include "libnfs-raw-mount.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define USEC_PER_SEC 1000000L -#define CRASH_DIR "/var/crash" -#define LOG_FILE "/tmp/vmcore_collect.log" -#define LOCK_FILE "/var/lock/vmcore_collect.pid" -#define MOUNT_INFO_FILE "/proc/self/mountinfo" -#define ERR_LOG(fmt, args...) err_log(__LINE__, fmt, ##args) -#define CONF_FILE_1 "/etc/vmcore_collect.conf" -#define CONF_FILE_2 "panic/config.txt" -#define UPLOAD_FILE ".uploaded" -#define FLAGS_LOG 2 -#define BUFSIZE 1024*1024 -#define PR_CONF(key, args...) snprintf(gconf.key, sizeof(gconf.key), ##args) - - -struct cv_config_t { - char crashdir[128]; - char hostname[128]; - char nfs_ip[64]; - char nfs_dir[128]; - FILE *fp; -} gconf; - - -static int gerrcode = 0; -static int glockfd = -1; -const char *file_list[] = {"vmcore", "vmcore-incomplete", "vmcore-dmesg.txt", UPLOAD_FILE}; - -struct nfs_context *nfs = NULL; -struct nfsfh *nfsfh = NULL; -struct nfs_url *url = NULL; - -static int get_disk_usage(const char path[]); -int mount_nfs(char* dir, int is_dir); -int nfs_copy(int fd, int size, char* file); -int64_t get_now_time() -{ - struct timeval tv; - gettimeofday(&tv, 0); - return USEC_PER_SEC * tv.tv_sec + tv.tv_usec; -} - -void trim_string(char *val, char found) -{ - char *end = val + strlen(val) - 1; - - while (end >= val && *end == found) - *end-- = '\0'; -} - -int endswith(char* str, char* p) -{ - int lens,lenp; - lens = strlen(str); - lenp = strlen(p); - - if(lenp <=0 || lens <= 0) - return -1; - - if(strcmp(str+lens-lenp,p) == 0) - return 0; - return -1; -} - -static ssize_t -file_pread(int fd, char *buf, size_t count, off_t off) -{ - lseek(fd, off, SEEK_SET); - return read(fd, buf, count); -} - -static ssize_t -file_pwrite(char *buf, size_t count, off_t off) -{ - return nfs_pwrite(nfs, nfsfh, off, count, buf); -} - -void err_log(int line, const char *fmt, ...) -{ - int n; - struct timeval tv; - char b[256]; - - gettimeofday(&tv, 0); - strftime(b, sizeof(b), "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec)); - n = strlen(b); - n += snprintf(b + n, sizeof(b) - n, ".%06ld [%d] - ", tv.tv_usec, line); - - va_list args; - va_start(args, fmt); - vsnprintf(b + n, sizeof(b) - n , fmt, args); - va_end(args); - fprintf(gconf.fp, b); - fflush(gconf.fp); -} - -time_t convert_time(const char *str) -{ - char *p; - struct tm tm; - int len; - - if ((p = strchr(str, '-')) == NULL) - return 0; - - p ++; - len = strlen(str) - (p - str); - - if (len != 19) - return 0; - - memset(&tm, 0, sizeof(tm)); - - if (sscanf(p, "%04d-%02d-%02d-%02d:%02d:%02d", - &tm.tm_year, &tm.tm_mon, &tm.tm_mday, - &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) - return 0; - - tm.tm_mon--; - tm.tm_isdst = -1; - tm.tm_year -= 1900; - return mktime(&tm); -} - -uint32_t get_local_addr() -{ - int fd = -1; - uint32_t ret = 0; - struct sockaddr_in addr; - socklen_t addrlen = sizeof(addr); - - addr.sin_family = AF_INET; - addr.sin_port = 165; - addr.sin_addr.s_addr = inet_addr("30.30.30.30"); - - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) - goto error_exit; - - if (connect(fd, (struct sockaddr *) &addr, addrlen) < 0) - goto error_exit; - - if (getsockname(fd, (struct sockaddr *) &addr, &addrlen) < 0) - goto error_exit; - - ret = addr.sin_addr.s_addr; -error_exit: - - if (fd >= 0) - close(fd); - - return ret; -} - - - - -void check_dir(const char *dirpath) -{ - DIR *dirp; - struct dirent *direntp; - time_t core_time = 0; - struct stat st; - char tmp[256]; - char skip[256]; - char dst_dmesg[256]; - char nowstr[32]; - int skip_flag; - int off = 0; - int count; - int fd,n; - int i; - - if ((dirp = opendir(dirpath)) == NULL) - return; - - while ((direntp = readdir(dirp)) != NULL) { - snprintf(tmp, sizeof(tmp), "%s/%s", dirpath, direntp->d_name); - printf("check %s\n",tmp); - - if (memcmp(direntp->d_name, ".", 2) == 0 || - memcmp(direntp->d_name, "..", 3) == 0) - continue; - - if (stat(tmp, &st) || S_ISDIR(st.st_mode) == 0) - continue; - - core_time = convert_time(direntp->d_name); - - if (core_time == 0) { - ERR_LOG("skip: %s\n", tmp); - continue; - } - - snprintf(skip, sizeof(skip), "%s/%s", tmp, UPLOAD_FILE); - skip_flag = ((stat(skip, &st) == 0) ? 1 : 0); - printf("check skip : %s skip_flag:%d\n",skip,skip_flag); - if (skip_flag == 1) - continue; - - //add_core_dir(core_time, tmp, skip_flag); - //src:tmp/vmcore-dmesg.txt, dst:url->path/*-dmesg.txt - strftime(nowstr, sizeof(nowstr), "%Y%m%d%H%M%S", localtime(&core_time)); - snprintf(tmp, sizeof(tmp), "%s_%s", nowstr, gconf.hostname); - printf("mount : %s\n",tmp); - if (mount_nfs(tmp,1) != 0) - { - printf("mount nfs failed! file:%s\n",tmp); - exit(1); - } - for(i = 0; i < 3; i++) - { - snprintf(dst_dmesg, sizeof(dst_dmesg), "%s/%s/%s", dirpath, direntp->d_name, file_list[i]); - printf("check vmcore : %s\n",dst_dmesg); - if(stat(dst_dmesg, &st) != 0) - continue; - - if((fd = open(dst_dmesg, O_RDONLY, 0660)) == -1) - { - printf("can not open file %s to copy to nfs\n", dst_dmesg); - continue; - } - - - snprintf(dst_dmesg, sizeof(dst_dmesg), "%s/%s", tmp, file_list[i]); - printf("mount : %s\n",dst_dmesg); - if( mount_nfs(dst_dmesg,0) != 0) - { - printf("mount nfs failed! file:%s\n",dst_dmesg); - exit(1); - } - - printf("ready to copy file %s to nfs nfs://%s/%s%s\n",dst_dmesg,url->server,url->path,dst_dmesg); - //TODO copy file - off = nfs_copy(fd,st.st_size,dst_dmesg); - if(off >= st.st_size) - { - printf("succeed to copy file %s/%s/* to %s\n",dirpath, direntp->d_name,tmp); - snprintf(skip, sizeof(skip), "%s/%s/%s",dirpath, direntp->d_name, UPLOAD_FILE); - n = open(skip, O_CREAT | O_WRONLY, 0644); - close(n); - } - printf("copied %d bytes\n", (int)off); - } - - } - - closedir(dirp); -} - -void deckey(char *str) -{ - char *p = str; - - while (*p) { - if (*p >= 'A' && *p <= 'Z') *p += 32; - else if (*p >= 'a' && *p <= 'z') *p -= 32; - - p++; - } -} - -void parse_config(char *conf) -{ -#define SET_STRING(name, key, val) if (strcmp(#name, key)==0) \ - snprintf(gconf.name, sizeof(gconf.name), "%s", val); -#define SET_INT(name, key, val) if(strcmp(#name, key)==0) \ - gconf.name = atoi(val); - - char *end, *key, *val; - key = conf; - - while (key) { - end = strchr(key, '\n'); - - if (end) - *end++ = '\0'; - - val = strchr(key, '='); - - if (val) { - *val++ = '\0'; - trim_string(key, ' '); - trim_string(key, '\t'); - SET_STRING(nfs_ip, key, val); - SET_STRING(nfs_dir, key, val); - } - - key = end; - } - -#undef SET_STRING -#undef SET_INT -} - -int split_column(char *buffer, char **column, int size) -{ - int idx = 0; - char *p = buffer; - - column[idx++] = p; - - while (*p && idx < size) { - if (*p == ' ' || *p == '\n') { - *p++ = '\0'; - column[idx++] = p; - } - - p++; - } - - return idx; -} - - -void scan_vmcore() -{ - FILE *fp; - char *column[16]; - char buffer[1024]; - char tmpdir[256]; - int cnt, i; - - - fp = fopen(MOUNT_INFO_FILE, "rb"); - memset(buffer, 0, sizeof(buffer)); - - while (fgets(buffer, sizeof(buffer), fp)) { - cnt = split_column(buffer, column, 16); - - for (i = 0; i < cnt; i++) { - if (memcmp(column[i], "-", 2) == 0) - break; - } - - if (i < 5 || i + 2 >= cnt) - continue; - - if (memcmp(column[i + 2], "/dev/", 5)) - continue; - - trim_string(column[4], '/'); - snprintf(tmpdir, sizeof(tmpdir), "%s%s", column[4], gconf.crashdir); - ERR_LOG("DISK: %s %s, crashdir: %s\n", column[i + 2], column[4], tmpdir); - check_dir(tmpdir); - } - - fclose(fp); - -} - - - -char *get_conf_text() -{ - int fd; - char *ret; - struct stat st; - - fd = open(CONF_FILE_1, O_RDONLY); - - if (fd < 0) - return NULL; - - fstat(fd, &st); - ret = (char *)malloc(st.st_size + 1); - - if (ret) { - read(fd, ret, st.st_size); - ret[st.st_size] = '\0'; - } - - close(fd); - - return ret; -} - -static void parse_local_conf() -{ - char *conf; - if ((conf = get_conf_text()) != NULL) { - parse_config(conf); - free(conf); - } -} - -int run_loop() -{ - ERR_LOG("START.\n"); - - parse_local_conf(); - scan_vmcore(); - - ERR_LOG("END.\n"); - return 0; -} - -void get_crash_dir() -{ - const char *fstype = "path "; - FILE *fp; - char *p; - char buffer[256]; - - fp = fopen("/etc/kdump.conf", "rb"); - - if (!fp) - return; - - while (fgets(buffer, sizeof(buffer), fp)) { - if (memcmp(buffer, fstype, strlen(fstype)) != 0) - continue; - - p = strchr(buffer, '\n'); - - if (p != NULL) - *p = '\0'; - - snprintf(gconf.crashdir, sizeof(gconf.crashdir), - "%s", buffer + strlen(fstype)); - break; - } - - fclose(fp); -} - -void init_default() -{ -//#define PR_CONF(key, args...) snprintf(gconf.key, sizeof(gconf.key), ##args) - char tmp[256]; - uint32_t ip = get_local_addr(); - uint8_t *arr = (uint8_t *)&ip; - - memset(&gconf, 0, sizeof(gconf)); - gconf.fp = stdout; - PR_CONF(crashdir, CRASH_DIR); - //PR_CONF(bucket_name, "kernel-khotfix"); - //PR_CONF(endpoint, "cn-hangzhou.oss.aliyun-inc.com"); - - if (ip) - PR_CONF(hostname, "%d.%d.%d.%d", arr[0], arr[1], arr[2], arr[3]); - else if (gethostname(tmp, sizeof(tmp)) == 0) - PR_CONF(hostname, "%s", tmp); - else - PR_CONF(hostname, "hostname"); - - get_crash_dir(); -} - - - -void main_destroy() -{ - - if (gconf.fp != stdout) - fclose(gconf.fp); - - if (glockfd >= 0) - close(glockfd); - -} - -void check_flock() -{ - int ret = -1; - - glockfd = open(LOCK_FILE, O_CREAT | O_RDWR, 0644); - - if (glockfd >= 0) - ret = lockf(glockfd, F_TLOCK, 0); - - if (ret < 0) { - printf("Lock failure: %s\n", LOCK_FILE); - - if (glockfd >= 0) - close(glockfd); - - exit(1); - } - - -} - -int mount_nfs(char* dir, int is_dir) -{ - int err = 0; - char nfs_url[512]; - sprintf(nfs_url,"nfs://%s/%s/%s",gconf.nfs_ip,gconf.nfs_dir,dir); - nfs = nfs_init_context(); - int ret; - if (nfs == NULL) - { - printf("failed to init context\n"); - err = -1; - goto finished; - } - - if ((url=nfs_parse_url_full(nfs, nfs_url)) == NULL) - { - printf("failed to parse url. %s\n",nfs_get_error(nfs)); - err = -1; - goto finished; - } - - if (nfs_mount(nfs, url->server, url->path) != 0) - { - printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); - err = -1; - goto finished; - } - - if (is_dir == 1) - { - if(nfs_mkdir(nfs,url->file) != 0) - { - printf("Failed to mkdir : %s\n", url->file); - err = -1; - goto finished; - } - } - else - { - if ((ret = nfs_open(nfs, url->file, 0600, &nfsfh)) != 0) - { - printf("failed to open %s and create now.\n", url->file); - if((ret = nfs_create(nfs,url->file,O_WRONLY|O_CREAT|O_EXCL,0660,&nfsfh)) != 0) - { - printf("create new file %s failed.\n", url->file); - err = -1; - goto finished; - } - } - } - - printf("succeed to mount to %s\n", nfs_url); - -finished : - nfs_destroy_url(url); - if (nfs != NULL) - { - if (nfsfh) - { - nfs_close(nfs, nfsfh); - nfsfh = NULL; - } - nfs_destroy_context(nfs); - } - return err; -} - -int nfs_copy(int fd, int size, char* file) -{ - int off = 0; - int count; - char buf[BUFSIZE]; - char nfs_url[512]; - sprintf(nfs_url,"nfs://%s/%s/%s",gconf.nfs_ip,gconf.nfs_dir,file); - nfs = nfs_init_context(); - int ret; - if (nfs == NULL) - { - printf("failed to init context\n"); - off = -1; - goto finished; - } - - if ((url=nfs_parse_url_full(nfs, nfs_url)) == NULL) - { - printf("failed to parse url. %s\n",nfs_get_error(nfs)); - off = -1; - goto finished; - } - - if (nfs_mount(nfs, url->server, url->path) != 0) - { - printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); - off = -1; - goto finished; - } - if ((ret = nfs_open(nfs, url->file, 0600, &nfsfh)) != 0) - { - printf("failed to open %s and create now.\n", url->file); - } - - while (off < size) { - count = (size_t)(size - off); - if (count > BUFSIZE) { - count = BUFSIZE; - } - count = file_pread(fd, buf, count, off); - if (count < 0) { - fprintf(stderr, "Failed to read from source file\n"); - break; - } - count = file_pwrite(buf, count, off); - if (count < 0) { - fprintf(stderr, "Failed to write to dest file\n"); - break; - } - - off += count; - } - close(fd); -finished : - nfs_destroy_url(url); - if (nfs != NULL) - { - if (nfsfh) - { - nfs_close(nfs, nfsfh); - nfsfh = NULL; - } - nfs_destroy_context(nfs); - } - return off; -} - -int main(int argc, char *argv[]) -{ - init_default(); - check_flock(); - run_loop(); - main_destroy(); - return gerrcode; -} - diff --git a/tools/vmcore/vmcore_collect.conf.example b/tools/vmcore/vmcore_collect.conf.example deleted file mode 100644 index a222a605c10cff4c1f4552454d00dffc22fa2827..0000000000000000000000000000000000000000 --- a/tools/vmcore/vmcore_collect.conf.example +++ /dev/null @@ -1,5 +0,0 @@ -#/etc/vmcore_collect.conf example - -#nfs_ip=YOUR_NFS_SERVER_IP -#nfs_dir=YOUR_NFS_SERVER_DIR - diff --git a/tools/vmcore/vmcore_const.py b/tools/vmcore/vmcore_const.py deleted file mode 100644 index 8f9ac84aa2f8930ddaa225e54311d4d1161ed14f..0000000000000000000000000000000000000000 --- a/tools/vmcore/vmcore_const.py +++ /dev/null @@ -1,25 +0,0 @@ -STATUS_HWERROR=5 -STATUS_SYSRQ=6 -# panic type -PANIC_UAF=1 -PANIC_DOUBLEFREE=2 -PANIC_OOBREAD=3 -PANIC_OOBWRITE=4 -PANIC_NULLPOINTER=5 -PANIC_UNINITVAR=6 -PANIC_STACKCORRUPTION=7 -PANIC_INVALIDIPPTR=8 -PANIC_INVALIDDATAPTR=9 -PANIC_BUGON=10 -PANIC_DIVIDEERROR=11 -PANIC_HARDLOCKUP=12 -PANIC_SOFTLOCKUP=13 -PANIC_HUNGTASK=14 -PANIC_RCUSTALL=15 - -BASEMODS = ['ext4','jbd2','overlay','libata','libiscsi','bridge','nf_conntrack','nf_conntrack_ipv4', - 'nf_nat','nf_nat_ipv4','iptable_nat','tun','binfmt_misc','xt_CHECKSUM','iptable_mangle', - 'nf_defrag_ipv4','xt_conntrack','ipt_REJECT','nf_reject_ipv4','stp','llc','ebtable_filter', - 'ebtables','ip6_tables','iptable_filter','iscsi_tcp','libiscsi_tcp','scsi_transport_iscsi', - 'bonding','dm_mod','sg','ip_tables','mbcache','sd_mod','mpt3sas','raid_class','scsi_transport_sas', - 'ahci','libahci','btrfs','zram','numa_balancer']