使用 C# ZabbixAPI 将模板添加到 Zabbix 主机
Add template to Zabbix host using C# ZabbixAPI
我正在尝试使用 this 特定的 nuget 包(版本 1.1.3)向 Zabbix (3.0) 中的主机添加模板。
虽然我能够 get/delete 特定的主机和模板,但我无法更新它们。
查看 Zabbix documentation for updating hosts 我发现了 templates 参数的描述:
Templates to replace the currently linked templates. Templates that are not passed are only unlinked.
所以我收集到我应该在主机对象中向 parentTemplates 属性 添加一个模板,并将主机传递给 Update HostService 的方法:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
hostService.Update(host);
(请注意,Context context = new Context()
可以工作,因为我使用的是 .config 文件。)
编译执行后,程序运行没有错误,但宿主仍然是无模板的
有人试过这个吗?
我是否漏掉了一些明显的东西?
Zabbix 配置注意事项:
- Template_test 是一个有效的现有模板
- testhost 是现有主机
- 我在此演示中使用的 Zabbix 用户具有 Zabbix 超级管理员权限以消除权限问题。
=== 编辑 ===
提出了四个请求:
- user.login(确定)
- template.get(确定)
- host.get(确定)
- host.update(挪威克朗)
最后一个问题。完整的请求是 here.
回复:
{"jsonrpc":"2.0","result":{"hostids":["10135"]},"id":"ca04d839-e6ec-4017-81b0-cc7f8e01fcfc"}
要求太大了。我会检查一下我是否可以 trim 降低一点。
问题评论中提到:参数名称无效。
这是因为 Zabbix host.get 方法 returns templates in parentTemplates 属性, while host.update 使用 模板.
NuGet 的创建者没有考虑到这一点,所以我在他的项目中创建了一个新的 issue。
我设法通过从 Host:
派生 class 使我的代码正常工作
private class NewHost : Host
{
public IList<Template> templates { get; set; }
}
然后我可以使用这个 class:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
var newhost = new NewHost
{
Id = host.Id,
templates = host.parentTemplates
}
hostService.Update(newhost);
因此,不仅使用了专有名称,而且请求正文也变得更小了。
我正在尝试使用 this 特定的 nuget 包(版本 1.1.3)向 Zabbix (3.0) 中的主机添加模板。
虽然我能够 get/delete 特定的主机和模板,但我无法更新它们。 查看 Zabbix documentation for updating hosts 我发现了 templates 参数的描述:
Templates to replace the currently linked templates. Templates that are not passed are only unlinked.
所以我收集到我应该在主机对象中向 parentTemplates 属性 添加一个模板,并将主机传递给 Update HostService 的方法:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
hostService.Update(host);
(请注意,Context context = new Context()
可以工作,因为我使用的是 .config 文件。)
编译执行后,程序运行没有错误,但宿主仍然是无模板的
有人试过这个吗? 我是否漏掉了一些明显的东西?
Zabbix 配置注意事项:
- Template_test 是一个有效的现有模板
- testhost 是现有主机
- 我在此演示中使用的 Zabbix 用户具有 Zabbix 超级管理员权限以消除权限问题。
=== 编辑 ===
提出了四个请求:
- user.login(确定)
- template.get(确定)
- host.get(确定)
- host.update(挪威克朗)
最后一个问题。完整的请求是 here.
回复:
{"jsonrpc":"2.0","result":{"hostids":["10135"]},"id":"ca04d839-e6ec-4017-81b0-cc7f8e01fcfc"}
要求太大了。我会检查一下我是否可以 trim 降低一点。
问题评论中提到:参数名称无效。 这是因为 Zabbix host.get 方法 returns templates in parentTemplates 属性, while host.update 使用 模板.
NuGet 的创建者没有考虑到这一点,所以我在他的项目中创建了一个新的 issue。
我设法通过从 Host:
派生 class 使我的代码正常工作private class NewHost : Host
{
public IList<Template> templates { get; set; }
}
然后我可以使用这个 class:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
var newhost = new NewHost
{
Id = host.Id,
templates = host.parentTemplates
}
hostService.Update(newhost);
因此,不仅使用了专有名称,而且请求正文也变得更小了。