【问题标题】:Add template to Zabbix host using C# ZabbixAPI使用 C# ZabbixAPI 将模板添加到 Zabbix 主机
【发布时间】:2018-08-14 09:34:50
【问题描述】:

我正在尝试使用 this 特定的 nuget 包(版本 1.1.3)将模板添加到 Zabbix (3.0) 中的主机。

虽然我能够获取/删除特定的主机和模板,但我无法更新它们。 看Zabbixdocumentation for updating hosts发现templates参数的描述是这样的:

用于替换当前链接模板的模板。未通过的模板仅取消链接。

所以我认为我应该在主机对象的 parentTemplates 属性中添加一个模板,并将主机传递给 HostServiceUpdate 方法>:

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 (NOK)

最后一个导致问题。完整的请求是here

回应:

{"jsonrpc":"2.0","re​​sult":{"hostids":["10135"]},"id":"ca04d839-e6ec-4017-81b0-cc7f8e01fcfc"}

请求太大了。我会看看能不能把它剪掉一点。

【问题讨论】:

  • 你能把发送到 Zabbix API 并由它返回的原始 JSON 发布吗?
  • @Richlv 给你。生成的请求是一场灾难......
  • 这是一个错误。库使用错误的参数名称。此issue 中的更多详细信息。

标签: c# api nuget zabbix


【解决方案1】:

如问题评论中所述:参数名称无效。 这是因为 Zabbix host.get 方法在 parentTemplates 属性中返回模板,而 host.update 使用 templates.

NuGet 的创建者没有考虑到这一点,所以我在他的项目中创建了一个新的issue

我设法通过从 Host 派生一个类来让我的代码工作:

private class NewHost : Host
{
    public IList<Template> templates { get; set; }
}

然后我可以使用这个类发出请求:

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);

因此,不仅使用了正确的名称,而且请求正文也更小了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多