主机名显示 IP+主机名的问题已经有更好的解决办法,参见《zabbix4.0蛇皮走位之主机名称(hostname)加入ip地址》



近期在部署zabbix时发现一个问题,在首页的仪表盘中显示的是hostname,很多如果没改默认主机名根本不知道是那台机器报警

本人版本为zabbix4.0.5

修改后的效果:

clipboard.png

需求加入IP信息:
实现方式:
1、修改主机详情中的可见名称

手工修改(不推荐,主机多时不方便,而且IP变了之后麻烦);
在数据库中建触发器或者定时任务(修改hosts表的name字段,ip信息在interface表中)

2、直接修改php文件

本人不会php,参考https://blog.csdn.net/xsm5223508/article/details/78696993这篇文章中的内容实现

修改文件 
/usr/share/zabbix/app/views/monitoring.widget.problems.view.php
注意备份原文件.

原代码

$table = (new CTableInfo())
    ->setHeader(array_merge($header, [
        $show_recovery_data ? _('Recovery time') : null,
        $show_recovery_data ? _('Status') : null,
        _('Info'),
        ($data['sortfield'] === 'host') ? [_('Host'), $sort_div] : _('Host'),
        [
            ($data['sortfield'] === 'name') ? [_('Problem'), $sort_div] : _('Problem'),
            ' • ',
            ($data['sortfield'] === 'severity') ? [_('Severity'), $sort_div] : _('Severity')
        ],
        $data['fields']['show_latest_values'] ? _('Latest values') : null,
        _('Duration'),
        _('Ack'),
        _('Actions'),
        $data['fields']['show_tags'] ? _('Tags') : null
    ]));

修改后代码:

$table = (new CTableInfo())
    ->setHeader(array_merge($header, [
        $show_recovery_data ? _('Recovery time') : null,
        $show_recovery_data ? _('Status') : null,
        _('Info'),
        _('Group'), //加一个群组
        ($data['sortfield'] === 'host') ? [_('Host'), $sort_div] : _('Host'),
        _('Interface'), //加一个ip
        [
            ($data['sortfield'] === 'name') ? [_('Problem'), $sort_div] : _('Problem'),
            ' • ',
            ($data['sortfield'] === 'severity') ? [_('Severity'), $sort_div] : _('Severity')
        ],
        $data['fields']['show_latest_values'] ? _('Latest values') : null,
        _('Duration'),
        _('Ack'),
        _('Actions'),
        $data['fields']['show_tags'] ? _('Tags') : null
    ]));

$table->addRow(array_merge($row, [

以上加入代码


    $hostid = $trigger['hosts'][0]['hostid'];
    $groups = API::HostGroup()->get([
        'output' => ['groupid', 'name'],
        'groupids' => null,
        'hostids' => $hostid,
        'monitored_hosts' => true,
        'preservekeys' => true
    ]);
    $group = array_shift($groups);
    $group = new CCol($group['name']);


    $hostinterfaces = API::HostInterface()->get([
        'output' => ['interfaceid', 'ip'],
        'interfaceids' => null,
        'hostids' => $hostid
    ]);
    $hostinterfaces = array_shift($hostinterfaces);
    $hostinterfaces = new CCol($hostinterfaces['ip']);

在table的数据中加入代码

$table->addRow(array_merge($row, [
        $show_recovery_data ? $cell_r_clock : null,
        $show_recovery_data ? $cell_status : null,
        makeInformationList($info_icons),
        $group,
        $triggers_hosts[$trigger['triggerid']],
        $hostinterfaces,
        $description,
        $data['fields']['show_latest_values'] ? CScreenProblem::getLatestValues($trigger['items']) : null,
        (new CCol(zbx_date2age($problem['clock'], ($problem['r_eventid'] != 0) ? $problem['r_clock'] : 0)))
            ->addClass(ZBX_STYLE_NOWRAP),
        (new CLink($problem['acknowledged'] == EVENT_ACKNOWLEDGED ? _('Yes') : _('No'), $problem_update_url))
            ->addClass($problem['acknowledged'] == EVENT_ACKNOWLEDGED ? ZBX_STYLE_GREEN : ZBX_STYLE_RED)
            ->addClass(ZBX_STYLE_LINK_ALT),
        makeEventActionsIcons($problem['eventid'], $data['data']['actions'], $data['data']['mediatypes'],
            $data['data']['users'], $data['config']
        ),
        $data['fields']['show_tags'] ? $data['data']['tags'][$problem['eventid']] : null
    ]));

问题:
这样改只是仪表盘中改了,在问题的菜单中并没有,希望哪位能补充一下


soft_xiang
38 声望5 粉丝

引用和评论

0 条评论