0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSVファイルを読み込んでZabbixにホスト登録

Last updated at Posted at 2023-11-06

1. はじめに

GUI画面で1サーバずつ登録していたのですが、非常に面倒なので、ZabbixAPIとpyzabbixを使用して、一括で登録してみました。Githubにリポジトリ作成していますが、もう少し詳細にして、日本語で記載してみました。
登録するのは、ZabbixAgentでの監視を前提として、ホストグループと登録するテンプレートは固定としています。

2. 使用する環境

以下を使用します。

  • Zabbix 6.0
  • python 3.11
  • pyzabbix
  • pandas

3. 環境準備

Zabbixは以下のURLを参照して構築してください。
https://www.zabbix.com/download?zabbix=6.0&os_distribution=alma_linux&os_version=9&components=server_frontend_agent&db=mysql&ws=apache

構築が完了したら、以下のコマンドを実行して、パッケージのインストールと
githubからクローンします。

dnf install python
pip install pyzabbix pandas
git clone https://github.com/doublethink-bps/zabbixHostregist.git

コマンドの実行が完了したら、CSVファイルを用意します。
CSVファイルにはホスト名とIPアドレスの情報を記載します。

hostname,IP1,IP2,IP3,IP4,IP5,IP6

例として、以下の通りとなります。

hostname,IP1,IP2,IP3,IP4,IP5,IP6
test01,,192.168.4.2,1.1.1.102,172.231.4.10,10.176.3.56,
test02,10.1.1.2,,1.1.1.103,,10.176.3.57,

CSVのファイルの用意が完了したら、hostRegist.pyを実行します。

4. 実装

まず、Zabbixのログイン情報、CSVファイルパス、テンプレートID、グループIDを指定します。

url = "http://192.168.3.19/zabbix"
user = "Admin"
password = "zabbix"
csvPath = "./zabbixHostinfo.csv"
templateid=10577
groupid=21

pyzabbixでZabbixAPIを呼び出し、ログインします。

zabbix = pyzabbix.ZabbixAPI(url)
zabbix.login(user,password)

テンプレートIDとグループIDはSQLかZabbix APIを実行して確認する必要があります。
以下はpyzabbixを使用して、Zabbix APIを実行して確認する例です。

zabbix = pyzabbix.ZabbixAPI("ZabbixのwebコンソールURL")
zabbix.login("Zabbixのwebコンソールユーザ","Zabbixのwebコンソールパスワード")
template = zabbix.template.get(
    output=["host","templateid"],
    filter={
	 # 適用したいテンプレート名
        "host":["テンプレート名"]
    }
)
group= zabbix.hostgroup.get(
    output=["groupid","name"],
    filter={
	# 適用したいホストグループ名
        "name":["ホストグループ名"]
    }
)
print(template[0])
print(group[0])

CSVファイルを一行ずつ読みだして、処理します。
また、Zabbixは各ホストに対し、複数インタフェースを登録する場合、どのインタフェースをデフォルトで使用するのか設定する必要があるため、最初に登録されるインタフェースをデフォルトとするために、IP1以外は、main関数内で条件分岐させ、interfacesリストが空の場合、mainParamへ1を代入し、最初にinterfacesリストに追加されたインタフェースをデフォルトとして設定します。

# read csv file for every one row
    for rows in df.iterrows():
        row = rows[1]
        interfaces = []
        # If "IP1" item isn't nan, 
        # add the interface infomatin to the [interfaces] list and assign 0 to [mainParam]
        if(pandas.notna(row["IP1"])):
            interfaces.append(createInterface(row["IP1"],1))
            mainParam=0
        # If "IP2" item isn't nan and [interfaces] isn't empty, 
        # add the interface infomatin to the [interfaces] list and assign 0 to [mainParam]
        if(pandas.notna(row["IP2"])):
            if(interfaces == []):
                mainParam=1
            interfaces.append(createInterface(row["IP2"],mainParam))
            mainParam=0
        # If "IP3" item isn't nan and [interfaces] isn't empty, 

分岐が終わったら、ホストの登録を行い、最後にホストのインタフェース情報が格納されたinterfacesリストをリセットし、次のホスト登録処理を開始します。

        createHost(row["hostname"],interfaces)
        # When finish registration host to zabbix, empty [interfaces] list 
        interfaces.clear()
def createHost(host,interfaces):
    zabbix.host.create(
        host=host,
        interfaces=interfaces,
        groups=[{"groupid":groupid}],
        templates=[{"templateid":templateid}]
    )

実行すると、以下のようにCSVに記載されたホストが登録されます。
image.png

image.png

5.URL

■Github
https://github.com/doublethink-bps/zabbixHostregist

■Zabbix
https://www.zabbix.com/documentation/6.0/jp/manual/api

■pyzabbix
https://pypi.org/project/pyzabbix/

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?