1

I'm a totaly begginer when it comes with bash scripting, I'm still learning the first steps.

Now trying to export an graph rule o cacti with this script, but for some reason it gives me a variable error..

Here is the code source: https://gist.github.com/rb83/130ac34dc416d2800725b948714eba3e/revisions

!/bin/bash -eu

cmd_export="mysql -N --batch -h ORIGIN_DBSERVER -uORIGIN_DBUSER -pORIGIN_DBPASS ORIGIN_DB"
cmd_import="mysql -h DEST_DBSERVER -uDEST_DBUSER -pDEST_DBPASS DEST_DB"
export _snmp_query_name='Disk_Space'
export _graph_type_name='Disk_Space'
export_rule_items() {
  $cmd_export -e "SELECT
        sequence,
        operation,
        field,
        operator,
        pattern
   FROM plugin_autom8_graph_rule_items
  WHERE rule_id = (
    SELECT id
      FROM plugin_autom8_graph_rules
     WHERE name = '${Disk_Space}'
    );" > rule_items.txt
}

export_rule_match() {
 $cmd_export -e "SELECT sequence,operation,field,operator,
 pattern
  FROM plugin_autom8_match_rule_items
 WHERE rule_id = (
    SELECT id
      FROM plugin_autom8_graph_rules
     WHERE name = '${Disk_Space}'
    )
   AND rule_type=1;" > rule_match_items.txt
}

export_rule() {
 x="$($cmd_export -e "SELECT q.name,g.name
 FROM   plugin_autom8_graph_rules as rule
  JOIN  (snmp_query as q,snmp_query_graph as g)
    ON  (rule.snmp_query_id=q.id
    AND rule.graph_type_id=g.id)
 WHERE  rule.name = '${Disk_Space}'")"
 IFS=$'\t' read -r _snmp_query_name _graph_type_name <<<"${x}"
}

create_rule() {
  if [[ -z "${_snmp_query_name:-}" || -z "${_graph_type_name:-}" ]]; then
    echo "missing snmp query name or graph type name, abort"
    exit 1
  fi
  $cmd_import -e "INSERT INTO automation_graph_rules(
      name,
      snmp_query_id,
      graph_type_id)
  VALUES (
  '${Disk_Space}',
  (SELECT id
     FROM snmp_query
    WHERE name = '${_snmp_query_name}'),
  (SELECT id
     FROM snmp_query_graph
    WHERE name = '${_graph_type_name}')
  );" || exit 1
}

import_rule_items() {
  $cmd_import -e "LOAD DATA LOCAL INFILE 'rule_items.txt'
  INTO TABLE automation_graph_rule_items
  (sequence,
  operation,
  field,
  operator,
  pattern)
  SET rule_id=(SELECT id from automation_graph_rules WHERE name='${Disk_Space}');"
}

import_rule_match() {
  $cmd_import -e "LOAD DATA LOCAL INFILE 'rule_match_items.txt'
 INTO TABLE automation_match_rule_items (sequence,operation,field,operator,pattern)
 SET rule_id=(SELECT id from automation_graph_rules WHERE name='${Disk_Space}'),
    rule_type=1;"
}


rewrite_rule_matches() {
  sed -e 's/host\./h./' -e 's/host_template\./ht./' rule_match_items.txt
}

> export Disk_Space="${2}"

echo -e "\nWarning: Read the code, understand the code, have backups, know how to fix."
echo -e "If this blows up, you're on your own. Proceed ?\n"

select proceed in \
  "Yes, I know what I'm doing." \
  "No." ; do
  [[ "$proceed" = Yes* ]] && break;
  exit 1
done

case $1 in
  export)
    export_stuff
    ;;
  import)
    import_stuff
    ;;
  transit)
    export_rule
    export_rule_items
    export_rule_match
    create_rule
    rewrite_rule_matches
    import_rule_items
    import_rule_match
    ;;
  *)
    echo "unknown operation $1, abort"
    exit 1
    ;;
Community
  • 1
  • 1
Jhonatan M
  • 31
  • 2
  • As the message says, in line 86 you are trying to use a variable that have no value assigned. I bet the variable is `$2`, so probably you did not passed two parameters and the code did not checked for a valid input. – Poshi May 20 '20 at 11:35
  • Thank you, how did I miss that ;) – Jhonatan M Jun 05 '20 at 22:26

0 Answers0