From 6e25d6bbf07cdae0032e46eaa58fe14de5b2f3f7 Mon Sep 17 00:00:00 2001 From: lijia Date: Wed, 18 Sep 2019 20:45:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9D=BF=E6=8B=86=E5=88=86?= =?UTF-8?q?=E4=B8=BA4=E4=B8=AAroles=E5=88=86=E5=88=AB=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hosts.astana | 59 +++++++++++++++ .../files/tsg-monitor.service | 0 .../files/tsg-monitor.sh | 3 +- .../files/tsg_chassis_interface.json | 1 - .../tasks/main.yml | 0 .../templates/tsg_sn.json.j2 | 0 roles/tsg-cli-mcn2/files/tsg-monitor.service | 15 ++++ roles/tsg-cli-mcn2/files/tsg-monitor.sh | 74 +++++++++++++++++++ .../files/tsg_chassis_interface.json | 12 +++ roles/tsg-cli-mcn2/tasks/main.yml | 45 +++++++++++ roles/tsg-cli-mcn2/templates/tsg_sn.json.j2 | 3 + roles/tsg-cli-mcn3/files/tsg-monitor.service | 15 ++++ roles/tsg-cli-mcn3/files/tsg-monitor.sh | 74 +++++++++++++++++++ .../files/tsg_chassis_interface.json | 13 ++++ roles/tsg-cli-mcn3/tasks/main.yml | 45 +++++++++++ roles/tsg-cli-mcn3/templates/tsg_sn.json.j2 | 3 + roles/tsg-cli-mxn/files/tsg-monitor.sh | 5 +- tsg-cli-playbook.yml | 6 +- 18 files changed, 366 insertions(+), 7 deletions(-) create mode 100644 hosts.astana rename roles/{tsg-cli-mcn1-3 => tsg-cli-mcn1}/files/tsg-monitor.service (100%) rename roles/{tsg-cli-mcn1-3 => tsg-cli-mcn1}/files/tsg-monitor.sh (99%) rename roles/{tsg-cli-mcn1-3 => tsg-cli-mcn1}/files/tsg_chassis_interface.json (99%) rename roles/{tsg-cli-mcn1-3 => tsg-cli-mcn1}/tasks/main.yml (100%) rename roles/{tsg-cli-mcn1-3 => tsg-cli-mcn1}/templates/tsg_sn.json.j2 (100%) create mode 100644 roles/tsg-cli-mcn2/files/tsg-monitor.service create mode 100644 roles/tsg-cli-mcn2/files/tsg-monitor.sh create mode 100644 roles/tsg-cli-mcn2/files/tsg_chassis_interface.json create mode 100644 roles/tsg-cli-mcn2/tasks/main.yml create mode 100644 roles/tsg-cli-mcn2/templates/tsg_sn.json.j2 create mode 100644 roles/tsg-cli-mcn3/files/tsg-monitor.service create mode 100644 roles/tsg-cli-mcn3/files/tsg-monitor.sh create mode 100644 roles/tsg-cli-mcn3/files/tsg_chassis_interface.json create mode 100644 roles/tsg-cli-mcn3/tasks/main.yml create mode 100644 roles/tsg-cli-mcn3/templates/tsg_sn.json.j2 diff --git a/hosts.astana b/hosts.astana new file mode 100644 index 0000000..31871c0 --- /dev/null +++ b/hosts.astana @@ -0,0 +1,59 @@ +[all:vars] +ansible_user=root +rpm_file_name=tsg-cli-1.0.6-1.el7.x86_64.rpm + +[blade-00] +10.4.39.9 +#10.4.39.13 +10.4.39.17 +10.4.39.21 +10.4.39.25 +10.4.39.29 +10.4.39.33 + +[blade-01] +10.4.39.10 +#10.4.39.14 +10.4.39.18 +10.4.39.22 +10.4.39.26 +10.4.39.30 +10.4.39.34 + +[blade-02] +10.4.39.11 +#10.4.39.15 +10.4.39.19 +10.4.39.23 +10.4.39.27 +10.4.39.31 +10.4.39.35 + +[blade-03] +10.4.39.12 +#10.4.39.16 +10.4.39.20 +10.4.39.24 +10.4.39.28 +10.4.39.32 +10.4.39.36 + +[astana-adc-3] +10.4.39.9 +10.4.39.10 +10.4.39.11 +10.4.39.12 + +[astana-adc-9] +10.4.39.33 +10.4.39.34 +10.4.39.35 +10.4.39.36 + +[control-blade-all:children] +blade-01 +blade-02 +blade-03 + +[certstore] +10.4.35.1 ansible_user=tsg diff --git a/roles/tsg-cli-mcn1-3/files/tsg-monitor.service b/roles/tsg-cli-mcn1/files/tsg-monitor.service similarity index 100% rename from roles/tsg-cli-mcn1-3/files/tsg-monitor.service rename to roles/tsg-cli-mcn1/files/tsg-monitor.service diff --git a/roles/tsg-cli-mcn1-3/files/tsg-monitor.sh b/roles/tsg-cli-mcn1/files/tsg-monitor.sh similarity index 99% rename from roles/tsg-cli-mcn1-3/files/tsg-monitor.sh rename to roles/tsg-cli-mcn1/files/tsg-monitor.sh index 3b15808..b8ca53a 100644 --- a/roles/tsg-cli-mcn1-3/files/tsg-monitor.sh +++ b/roles/tsg-cli-mcn1/files/tsg-monitor.sh @@ -1,6 +1,6 @@ #!/bin/sh -#mcn1-3 +#mcn1 #防止因上一次的命令阻塞或长时间未返回, #导致10秒内还不结束, 强行杀掉, @@ -9,7 +9,6 @@ killall_uncompleted_cmd(){ #killall -9 tsg_cluster_register killall -9 tsg_diagnose_background killall -9 tsg_update_tags - killall -9 tsg_monit_interface } diff --git a/roles/tsg-cli-mcn1-3/files/tsg_chassis_interface.json b/roles/tsg-cli-mcn1/files/tsg_chassis_interface.json similarity index 99% rename from roles/tsg-cli-mcn1-3/files/tsg_chassis_interface.json rename to roles/tsg-cli-mcn1/files/tsg_chassis_interface.json index 9f84b50..db4dd03 100644 --- a/roles/tsg-cli-mcn1-3/files/tsg_chassis_interface.json +++ b/roles/tsg-cli-mcn1/files/tsg_chassis_interface.json @@ -8,6 +8,5 @@ "dev_type": "marsio", "flow_type": "mirror" } - ] } diff --git a/roles/tsg-cli-mcn1-3/tasks/main.yml b/roles/tsg-cli-mcn1/tasks/main.yml similarity index 100% rename from roles/tsg-cli-mcn1-3/tasks/main.yml rename to roles/tsg-cli-mcn1/tasks/main.yml diff --git a/roles/tsg-cli-mcn1-3/templates/tsg_sn.json.j2 b/roles/tsg-cli-mcn1/templates/tsg_sn.json.j2 similarity index 100% rename from roles/tsg-cli-mcn1-3/templates/tsg_sn.json.j2 rename to roles/tsg-cli-mcn1/templates/tsg_sn.json.j2 diff --git a/roles/tsg-cli-mcn2/files/tsg-monitor.service b/roles/tsg-cli-mcn2/files/tsg-monitor.service new file mode 100644 index 0000000..f61aded --- /dev/null +++ b/roles/tsg-cli-mcn2/files/tsg-monitor.service @@ -0,0 +1,15 @@ +[Unit] +Description=tsg monitor service +Requires=network.target +After=network.target + +[Service] +#WorkingDirectory=/opt/tsg/tsg-monitor/ +ExecStart=/opt/tsg/tsg-monitor/tsg-monitor.sh +#ExecStop=/bin/kill -9 $MAINPID +Type=simple +Restart=always +RestartSec=5s + +[Install] +WantedBy=multi-user.target diff --git a/roles/tsg-cli-mcn2/files/tsg-monitor.sh b/roles/tsg-cli-mcn2/files/tsg-monitor.sh new file mode 100644 index 0000000..cc287a5 --- /dev/null +++ b/roles/tsg-cli-mcn2/files/tsg-monitor.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +#mcn2 + +#防止因上一次的命令阻塞或长时间未返回, +#导致10秒内还不结束, 强行杀掉, +#否则长时间运行后, 会有大量后台进程运行 +killall_uncompleted_cmd(){ + #killall -9 tsg_cluster_register + killall -9 tsg_diagnose_background + killall -9 tsg_update_tags + killall -9 tsg_monit_interface + +} + +start_background_cmd(){ + #后台并发运行, 保证所有命令的开始运行时间基本一样, + #且不会因某个命令网络拥塞、执行时间长等问题,阻塞while(1)主循环 + /opt/tsg/tsg-monitor/tsg_diagnose_background > /dev/null & + /opt/tsg/tsg-monitor/tsg_update_tags > /dev/null & + /opt/tsg/tsg-monitor/tsg_monit_interface > /dev/null & +} + +#return value: current time in ms +get_current_time_in_ms(){ + time_sec=`date +"%s"` + time_nsec=`date +"%N"` + #echo $time_sec + #echo $time_nsec + + time_epoch_ms=`echo | awk -v a=$time_sec -v b=$time_nsec '{printf("%.f"), a*1000+b/1000/1000}'` + + echo $time_epoch_ms +} + +#args: +#begin from time, in ms +#wait for n ms +sleep_for_time_ms(){ + last_time=$1 + wait_sec=$2 + #break_time=`echo | awk -v a=$last_time -v b=$wait_sec '{printf("%.f"), a+b}'` + break_time=`expr $last_time + $wait_sec` + break_time_int=`expr $break_time` + #echo "start: last_time is:$last_time, expect break timeis:$break_time!" + + current_time=0 + break_time=0 + #break_time=`expr $last_time + 1000*$1` + while [ 1 ]; do + current_time=`get_current_time_in_ms` + current_time_int=`expr $current_time` + if [ $current_time_int -ge $break_time_int ]; then + #echo "current is: $current_time_int, break_time is:$break_time_int, break!" + break + else + #echo "break is: $current_time_int, last_time is:$break_time_int, continue!" + # usleep is us + usleep 1000 + fi + done + +} + +while [ 1 ]; do + start_time=`get_current_time_in_ms` + echo tsg-monitor start at `date +"%Y/%m/%d, %H:%M:%S.%N"` >> /tmp/tsg-monitor.log + + start_background_cmd + sleep 10 + killall_uncompleted_cmd + + sleep_for_time_ms $start_time 15000 +done diff --git a/roles/tsg-cli-mcn2/files/tsg_chassis_interface.json b/roles/tsg-cli-mcn2/files/tsg_chassis_interface.json new file mode 100644 index 0000000..39c2d8f --- /dev/null +++ b/roles/tsg-cli-mcn2/files/tsg_chassis_interface.json @@ -0,0 +1,12 @@ +{ + "interface_list": [{ + "dev_name": "ens8f1", + "dev_type": "pcap", + "flow_type": "intercomm" + },{ + "dev_name": "ens8f2", + "dev_type": "marsio", + "flow_type": "mirror" + } + ] +} diff --git a/roles/tsg-cli-mcn2/tasks/main.yml b/roles/tsg-cli-mcn2/tasks/main.yml new file mode 100644 index 0000000..dff0f21 --- /dev/null +++ b/roles/tsg-cli-mcn2/tasks/main.yml @@ -0,0 +1,45 @@ +--- +- name: "copy tsg-cli rmp to destination server" + synchronize: + src: "{{ role_path }}/../tsg-common-files/{{ rpm_file_name }}" + dest: "/tmp/tsg-cli-deploy/" + +- name: "install tsg-cli" + yum: + name: "{{ packages }}" + state: present + vars: + packages: + - /tmp/tsg-cli-deploy/{{ rpm_file_name }} + +- name: Template the tsg_sn.json + template: + src: "{{ role_path }}/templates/tsg_sn.json.j2" + dest: /opt/tsg/etc/tsg_sn.json + tags: template + +- name: "copy tsg-monitor.service to destination server" + synchronize: + src: "{{ role_path }}/files/tsg-monitor.service" + dest: "/usr/lib/systemd/system" + +- name: "copy tsg_chassis_interface.json to destination server" + synchronize: + src: "{{ role_path }}/files/tsg_chassis_interface.json" + dest: "/opt/tsg/etc/" + +- name: "copy tsg-monitor.sh to destination server" + #synchronize: + copy: + src: "{{ role_path }}/files/tsg-monitor.sh" + dest: "/opt/tsg/tsg-monitor/" + mode: 0755 + +- name: "reload systemd config" + command: systemctl daemon-reload + +- name: "enable tsg-monitor service" + command: systemctl enable tsg-monitor.service + +#- name: "start tsg-monitor service" +# command: systemctl start tsg-monitor.service diff --git a/roles/tsg-cli-mcn2/templates/tsg_sn.json.j2 b/roles/tsg-cli-mcn2/templates/tsg_sn.json.j2 new file mode 100644 index 0000000..bceeb99 --- /dev/null +++ b/roles/tsg-cli-mcn2/templates/tsg_sn.json.j2 @@ -0,0 +1,3 @@ +{ + "sn": "{{ SN }}" +} \ No newline at end of file diff --git a/roles/tsg-cli-mcn3/files/tsg-monitor.service b/roles/tsg-cli-mcn3/files/tsg-monitor.service new file mode 100644 index 0000000..f61aded --- /dev/null +++ b/roles/tsg-cli-mcn3/files/tsg-monitor.service @@ -0,0 +1,15 @@ +[Unit] +Description=tsg monitor service +Requires=network.target +After=network.target + +[Service] +#WorkingDirectory=/opt/tsg/tsg-monitor/ +ExecStart=/opt/tsg/tsg-monitor/tsg-monitor.sh +#ExecStop=/bin/kill -9 $MAINPID +Type=simple +Restart=always +RestartSec=5s + +[Install] +WantedBy=multi-user.target diff --git a/roles/tsg-cli-mcn3/files/tsg-monitor.sh b/roles/tsg-cli-mcn3/files/tsg-monitor.sh new file mode 100644 index 0000000..dcd0ca5 --- /dev/null +++ b/roles/tsg-cli-mcn3/files/tsg-monitor.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +#mcn3 + +#防止因上一次的命令阻塞或长时间未返回, +#导致10秒内还不结束, 强行杀掉, +#否则长时间运行后, 会有大量后台进程运行 +killall_uncompleted_cmd(){ + #killall -9 tsg_cluster_register + killall -9 tsg_diagnose_background + killall -9 tsg_update_tags + killall -9 tsg_monit_interface + +} + +start_background_cmd(){ + #后台并发运行, 保证所有命令的开始运行时间基本一样, + #且不会因某个命令网络拥塞、执行时间长等问题,阻塞while(1)主循环 + /opt/tsg/tsg-monitor/tsg_diagnose_background > /dev/null & + /opt/tsg/tsg-monitor/tsg_update_tags > /dev/null & + /opt/tsg/tsg-monitor/tsg_monit_interface > /dev/null & +} + +#return value: current time in ms +get_current_time_in_ms(){ + time_sec=`date +"%s"` + time_nsec=`date +"%N"` + #echo $time_sec + #echo $time_nsec + + time_epoch_ms=`echo | awk -v a=$time_sec -v b=$time_nsec '{printf("%.f"), a*1000+b/1000/1000}'` + + echo $time_epoch_ms +} + +#args: +#begin from time, in ms +#wait for n ms +sleep_for_time_ms(){ + last_time=$1 + wait_sec=$2 + #break_time=`echo | awk -v a=$last_time -v b=$wait_sec '{printf("%.f"), a+b}'` + break_time=`expr $last_time + $wait_sec` + break_time_int=`expr $break_time` + #echo "start: last_time is:$last_time, expect break timeis:$break_time!" + + current_time=0 + break_time=0 + #break_time=`expr $last_time + 1000*$1` + while [ 1 ]; do + current_time=`get_current_time_in_ms` + current_time_int=`expr $current_time` + if [ $current_time_int -ge $break_time_int ]; then + #echo "current is: $current_time_int, break_time is:$break_time_int, break!" + break + else + #echo "break is: $current_time_int, last_time is:$break_time_int, continue!" + # usleep is us + usleep 1000 + fi + done + +} + +while [ 1 ]; do + start_time=`get_current_time_in_ms` + echo tsg-monitor start at `date +"%Y/%m/%d, %H:%M:%S.%N"` >> /tmp/tsg-monitor.log + + start_background_cmd + sleep 10 + killall_uncompleted_cmd + + sleep_for_time_ms $start_time 15000 +done diff --git a/roles/tsg-cli-mcn3/files/tsg_chassis_interface.json b/roles/tsg-cli-mcn3/files/tsg_chassis_interface.json new file mode 100644 index 0000000..b762b22 --- /dev/null +++ b/roles/tsg-cli-mcn3/files/tsg_chassis_interface.json @@ -0,0 +1,13 @@ +{ + "interface_list": [{ + "dev_name": "ens8f1", + "dev_type": "pcap", + "flow_type": "intercomm" + },{ + "dev_name": "ens8f2", + "dev_type": "marsio", + "flow_type": "mirror" + } + + ] +} diff --git a/roles/tsg-cli-mcn3/tasks/main.yml b/roles/tsg-cli-mcn3/tasks/main.yml new file mode 100644 index 0000000..dff0f21 --- /dev/null +++ b/roles/tsg-cli-mcn3/tasks/main.yml @@ -0,0 +1,45 @@ +--- +- name: "copy tsg-cli rmp to destination server" + synchronize: + src: "{{ role_path }}/../tsg-common-files/{{ rpm_file_name }}" + dest: "/tmp/tsg-cli-deploy/" + +- name: "install tsg-cli" + yum: + name: "{{ packages }}" + state: present + vars: + packages: + - /tmp/tsg-cli-deploy/{{ rpm_file_name }} + +- name: Template the tsg_sn.json + template: + src: "{{ role_path }}/templates/tsg_sn.json.j2" + dest: /opt/tsg/etc/tsg_sn.json + tags: template + +- name: "copy tsg-monitor.service to destination server" + synchronize: + src: "{{ role_path }}/files/tsg-monitor.service" + dest: "/usr/lib/systemd/system" + +- name: "copy tsg_chassis_interface.json to destination server" + synchronize: + src: "{{ role_path }}/files/tsg_chassis_interface.json" + dest: "/opt/tsg/etc/" + +- name: "copy tsg-monitor.sh to destination server" + #synchronize: + copy: + src: "{{ role_path }}/files/tsg-monitor.sh" + dest: "/opt/tsg/tsg-monitor/" + mode: 0755 + +- name: "reload systemd config" + command: systemctl daemon-reload + +- name: "enable tsg-monitor service" + command: systemctl enable tsg-monitor.service + +#- name: "start tsg-monitor service" +# command: systemctl start tsg-monitor.service diff --git a/roles/tsg-cli-mcn3/templates/tsg_sn.json.j2 b/roles/tsg-cli-mcn3/templates/tsg_sn.json.j2 new file mode 100644 index 0000000..bceeb99 --- /dev/null +++ b/roles/tsg-cli-mcn3/templates/tsg_sn.json.j2 @@ -0,0 +1,3 @@ +{ + "sn": "{{ SN }}" +} \ No newline at end of file diff --git a/roles/tsg-cli-mxn/files/tsg-monitor.sh b/roles/tsg-cli-mxn/files/tsg-monitor.sh index c93efa6..34acb56 100644 --- a/roles/tsg-cli-mxn/files/tsg-monitor.sh +++ b/roles/tsg-cli-mxn/files/tsg-monitor.sh @@ -15,7 +15,10 @@ start_background_cmd(){ #后台并发运行, 保证所有命令的开始运行时间基本一样, #且不会因某个命令网络拥塞、执行时间长等问题,阻塞while(1)主循环 /opt/tsg/tsg-monitor/tsg_cluster_register > /dev/null & - #nohup /opt/tsg/tsg-monitor/tsg_diagnose_background & + + #mxn板只检测cpu, mem, disk等, 前台cli命令启用diagnose, + #后台服务依靠oam snmp模块, 无需运行tsg_diagnose_background + #/opt/tsg/tsg-monitor/tsg_diagnose_background & /opt/tsg/tsg-monitor/tsg_update_tags > /dev/null & } diff --git a/tsg-cli-playbook.yml b/tsg-cli-playbook.yml index 167e9a4..1e66685 100644 --- a/tsg-cli-playbook.yml +++ b/tsg-cli-playbook.yml @@ -10,14 +10,14 @@ - hosts: sled-mcn1 roles: - - tsg-cli-mcn1-3 + - tsg-cli-mcn1 - hosts: sled-mcn2 roles: - - tsg-cli-mcn1-3 + - tsg-cli-mcn2 - hosts: sled-mcn3 roles: - - tsg-cli-mcn1-3 + - tsg-cli-mcn3