思路是通过cacti自带的命令行管理工具来实现,下面先列一下这几个命令的用法

[root@iZ2zecs4lwn56p5g1e7c63Z cli]# php add_device.php
Cacti Add Device Utility, Version 1.2.7 (DB: 1.2.7), Copyright (C) 2004-2019 The Cacti Group

usage: add_device.php --description=[description] --ip=[IP] --template=[ID] [--notes="[]"] [--disable]
[--poller=[id]] [--site=[id] [--external-id=[S]] [--proxy] [--threads=[1]
[--avail=[ping]] --ping_method=[icmp] --ping_port=[N/A, 1-65534] --ping_timeout=[N] --ping_retries=[2]
[--version=[0|1|2|3]] [--community=] [--port=161] [--timeout=500]
[--username= --password=] [--authproto=] [--privpass= --privproto=] [--context=] [--engineid=]
[--quiet]

Required:
--description the name that will be displayed by Cacti in the graphs
--ip self explanatory (can also be a FQDN)

Optional:
--proxy if specified, allows adding a second host with same ip address
--template 0, is a number (read below to get a list of templates)
--location '', The physical location of the Device.
--notes '', General information about this host. Must be enclosed using double quotes.
--external-id '', An external ID to align Cacti devices with devices from other systems.
--disable 0, 1 to add this host but to disable checks and 0 to enable it
--poller 0, numeric poller id that will perform data collection for the device.
--site 0, numeric site id that will be associated with the device.
--threads 1, numeric number of threads to poll device with.
--avail pingsnmp, [ping][none, snmp, pingsnmp, pingorsnmp]
--ping_method tcp, icmp|tcp|udp
--ping_port '', 1-65534
--ping_retries 2, the number of time to attempt to communicate with a host
--ping_timeout N, the ping timeout in milliseconds. Defaults to database setting.
--version 1, 0|1|2|3, snmp version. 0 for no snmp
--community '', snmp community string for snmpv1 and snmpv2. Leave blank for no community
--port 161
--timeout 500
--username '', snmp username for snmpv3
--password '', snmp password for snmpv3
--authproto '', snmp authentication protocol for snmpv3
--privpass '', snmp privacy passphrase for snmpv3
--privproto '', snmp privacy protocol for snmpv3
--context '', snmp context for snmpv3
--engineid '', snmp engineid for snmpv3
--max_oids 10, 1-60, the number of OIDs that can be obtained in a single SNMP Get request

List Options:
--list-host-templates
--list-communities
--quiet - batch mode value return

[root@iZ2zecs4lwn56p5g1e7c63Z
[root@iZ2zecs4lwn56p5g1e7c63Z cli]# php add_graphs.php
Cacti Add Graphs Utility, Version 1.2.7 (DB: 1.2.7), Copyright (C) 2004-2019 The Cacti Group

usage: add_graphs.php --graph-type=[cg|ds] --graph-template-id=[ID]
--host-id=[ID] [--graph-title=title] [graph options] [--force] [--quiet]

Cacti utility for creating graphs via a command line interface. This utility can
create both Data Query (ds) type Graphs as well as Graph Template (cg) type graphs.

For Non Data Query (cg) Graphs:
[--input-fields="[data-template-id:]field-name=value ..."] [--force]

--input-fields If your data template allows for custom input data, you may specify that
here. The data template id is optional and applies where two input fields
have the same name.
--force If you set this flag, then new cg graphs will be created, even though they
may already exist

For Data Query (ds) Graphs:
--snmp-query-id=[ID] --snmp-query-type-id=[ID] --snmp-field=[SNMP Field]
--snmp-value=[SNMP Value] | --snmp-value-regex=[REGEX]
[--graph-title=S] Defaults to what ever is in the Graph Template/Data Template.
[--reindex-method=N] The reindex method to be used for that data query.
NOTE: If Data Query is already associated, the reindex method will NOT be changed.

Valid --reindex-methos include
0|None = No reindexing
1|Uptime = Uptime goes Backwards (Default)
2|Index = Index Count Changed
3|Fields = Verify all Fields

NOTE: You may supply multiples of the --snmp-field and --snmp-value | --snmp-value-regex arguments.

List Options:
--list-hosts
--list-graph-templates [--host-template-id=[ID]]
--list-input-fields --graph-template-id=[ID]
--list-snmp-queries
--list-query-types --snmp-query-id [ID]
--list-snmp-fields --host-id=[ID] [--snmp-query-id=[ID]]
--list-snmp-values --host-id=[ID] [--snmp-query-id=[ID]] --snmp-field=[Field]

[root@iZ2zecs4lwn56p5g1e7c63Z
[root@iZ2zecs4lwn56p5g1e7c63Z cli]# php add_tree.php 
Cacti Add Tree Utility, Version 1.2.7 (DB: 1.2.7), Copyright (C) 2004-2019 The Cacti Group

usage: add_tree.php --type=[tree|node] [type-options] [--quiet]

Tree options:
--name=[Tree Name]
--sort-method=[manual|alpha|natural|numeric]

Node options:
--node-type=[header|site|host|graph]
--tree-id=[ID]
[--parent-node=[ID] [Node Type Options]]

Header node options:
--name=[Name]

Site node options:
--site-id=[ID]
Host node options:
--host-id=[ID]
[--host-group-style=[1|2]]
(host group styles:
1 = Graph Template,
2 = Data Query Index)

Graph node options:
--graph-id=[ID]

List Options:
--list-sites
--list-hosts
--list-trees
--list-nodes --tree-id=[ID]
--list-graphs --host-id=[ID]
[root@iZ2zecs4lwn56p5g1e7c63Z cli]#

介绍完了命令,我们开始写shell脚本。

#!/bin/bash
#cacti批量加监控
#脚本运行路径/var/www/html/cacti/cli/
#读取IP列表
MYFILE=/var/www/html/cacti/cli/ips.txt
while read host_info
do
host_name=$(echo $host_info |awk '{print $1}')
host_ip=$(echo $host_info |awk '{print $2}')
#添加cacti设备
php add_device.php --description="$host_name" --ip="$host_ip" --ping_method=ICMP --template=0 --version=2 --community="gsws"
#读取设备ID(gerp -w为精确匹配,否则会列出所有包含要查询ip的关键字,导致获取多个host-id,程序无法正常执行)
host_ids=`php add_graphs.php --list-hosts |grep -w $host_ip |awk '{print $1}'`
#创建模板图像
#php add_graphs.php --host-id="$host_ids" --graph-type=cg --graph-template-id=36
#创建网卡数据图像
php add_graphs.php --host-id="$host_ids" --graph-type=ds --graph-template-id=41 --snmp-query-id=4 --snmp-query-type-id=16 --snmp-field=ifOperStatus --snmp-value="Up"
#添加设备到相应的组
if [[ $host_name =~ "BGP" ]]
then
php add_tree.php --host-id="$host_ids" --type=node --node-type=host --tree-id=4
elif [[ $host_name =~ "24小时" ]]
then
php add_tree.php --host-id="$host_ids" --type=node --node-type=host --tree-id=2
elif [[ $host_name =~ "海外" ]]
then
php add_tree.php --host-id="$host_ids" --type=node --node-type=host --tree-id=3
elif [[ $host_name =~ "单线" ]]
then
php add_tree.php --host-id="$host_ids" --type=node --node-type=host --tree-id=5
else
php add_tree.php --host-id="$host_ids" --type=node --node-type=host --tree-id=6
fi
done < $MYFILE
##--脚本结束--##

脚本里面需要填写的ID,使用上面的命令都可以查到,根据自己的环境填写。

ips.txt文件的内容形式如下:

主机名1  IP1

主机名2  IP2