· 6 years ago · Mar 12, 2019, 06:22 PM
1#!/bin/bash -eu
2#
3# THIS IS A QUICK AND DIRTY SCRIPT - don't bring this close to shellcheck.
4#
5# Do not use this unless you understand it.
6#
7cmd_export="mysql -N --batch -h ORIGIN_DBSERVER -uORIGIN_DBUSER -pORIGIN_DBPASS ORIGIN_DB"
8cmd_import="mysql -h DEST_DBSERVER -uDEST_DBUSER -pDEST_DBPASS DEST_DB"
9export _snmp_query_name=''
10export _graph_type_name=''
11export_rule_items() {
12 $cmd_export -e "SELECT
13 sequence,
14 operation,
15 field,
16 operator,
17 pattern
18 FROM plugin_autom8_graph_rule_items
19 WHERE rule_id = (
20 SELECT id
21 FROM plugin_autom8_graph_rules
22 WHERE name = '${graph_rule_name}'
23 );" > rule_items.txt
24}
25
26export_rule_match() {
27 $cmd_export -e "SELECT sequence,operation,field,operator,
28 pattern
29 FROM plugin_autom8_match_rule_items
30 WHERE rule_id = (
31 SELECT id
32 FROM plugin_autom8_graph_rules
33 WHERE name = '${graph_rule_name}'
34 )
35 AND rule_type=1;" > rule_match_items.txt
36}
37
38export_rule() {
39 x="$($cmd_export -e "SELECT q.name,g.name
40 FROM plugin_autom8_graph_rules as rule
41 JOIN (snmp_query as q,snmp_query_graph as g)
42 ON (rule.snmp_query_id=q.id
43 AND rule.graph_type_id=g.id)
44 WHERE rule.name = '${graph_rule_name}'")"
45 IFS=$'\t' read -r _snmp_query_name _graph_type_name <<<"${x}"
46}
47
48create_rule() {
49 if [[ -z "${_snmp_query_name:-}" || -z "${_graph_type_name:-}" ]]; then
50 echo "missing snmp query name or graph type name, abort"
51 exit 1
52 fi
53 $cmd_import -e "INSERT INTO automation_graph_rules(
54 name,
55 snmp_query_id,
56 graph_type_id)
57 VALUES (
58 '${graph_rule_name}',
59 (SELECT id
60 FROM snmp_query
61 WHERE name = '${_snmp_query_name}'),
62 (SELECT id
63 FROM snmp_query_graph
64 WHERE name = '${_graph_type_name}')
65 );" || exit 1
66}
67
68import_rule_items() {
69 $cmd_import -e "LOAD DATA LOCAL INFILE 'rule_items.txt'
70 INTO TABLE automation_graph_rule_items
71 (sequence,
72 operation,
73 field,
74 operator,
75 pattern)
76 SET rule_id=(SELECT id from automation_graph_rules WHERE name='${graph_rule_name}');"
77}
78
79import_rule_match() {
80 $cmd_import -e "LOAD DATA LOCAL INFILE 'rule_match_items.txt'
81 INTO TABLE automation_match_rule_items (sequence,operation,field,operator,pattern)
82 SET rule_id=(SELECT id from automation_graph_rules WHERE name='${graph_rule_name}'),
83 rule_type=1;"
84}
85
86
87rewrite_rule_matches() {
88 sed -e 's/host\./h./' -e 's/host_template\./ht./' rule_match_items.txt
89}
90
91export graph_rule_name="${2}"
92
93echo -e "\nWarning: Read the code, understand the code, have backups, know how to fix."
94echo -e "If this blows up, you're on your own. Proceed ?\n"
95
96select proceed in \
97 "Yes, I know what I'm doing." \
98 "No." ; do
99 [[ "$proceed" = Yes* ]] && break;
100 exit 1
101done
102
103case $1 in
104 export) # merely export
105 export_rule
106 export_rule_items
107 export_rule_match
108 ;;
109 import) # this assumes the graph rule exists. won't rewrite match entries
110 rewrite_rule_matches
111 import_rule_items
112 import_rule_match
113 ;;
114 transit) #only this will create a graph rule
115 export_rule
116 export_rule_items
117 export_rule_match
118 create_rule
119 rewrite_rule_matches
120 import_rule_items
121 import_rule_match
122 ;;
123 *)
124 echo "unknown operation $1, abort"
125 exit 1
126 ;;
127esac