2021年9月29日 星期三

新版Cacti 如何import舊版的template ( How new Cacti import template of old Cacti)

這一題我好像解過,但久了又忘了,做一個記錄,避免久了再忘。

簡單來說,Cacti的所有template的xml檔都有一段:hash_xxoooo的文字。以下例而言,06代表功能,0024代表版本。

hash_06002419414480d6897c8731c7dc6c5310653e

我們可以簡單查詢0024代表的是0.8.8版

cat /usr/share/cacti/include/global_arrays.php  | grep 0024

        '0.8.8'  => '0024',

        '0.8.8a' => '0024',

        '0.8.8b' => '0024',

我們要從Cacti 1.2.18 import 這些template,就要修改這些hash的內容,要從0024改成0102 (0102是從下面的指令抓出來的),

cat /usr/share/cacti/include/global_arrays.php  | grep 1.2.18

        '1.2.18'  => '0102',

以此例而言,要把hash_060024 改成 hash_060102

但一個一個改太笨,可以的話當然是sed一行搞定,可惜我功力不強,試不出來。只好寫小程式來處理,範例如下。

#!/bin/bash

from=`grep -oe 'hash_..0024' test.xml | sort | uniq`

for value in $from

do

    to=`echo $value | sed 's/0024/0102/g'`

    sed -i "s/$value/$to/g" "test.xml"

done

PHP的版本:

<?php

 $version = "0102";

 $fp = file("test.xml");

 $myfile = fopen("output.xml", "w");

 foreach($fp as $line)

 {

  if(preg_match("/hash_/",$line))

  {

   $pos = strpos($line, "hash_");

   $orig = substr($line,$pos,11);

   $new = substr($line,$pos,7);

   $newstring = $new.$version;

   $line = str_replace($orig,$newstring,$line);

  }

  fwrite($myfile, $line);

 }

 fclose($myfile);

?>

如果要從cli import template的話,可以使用下列指令:

php /usr/share/cacti/cli/import_template.php --filename output.xml


沒有留言:

張貼留言