第三方系统 引入 zabbix 的主机视图 (免登录)

api 登录 zabbix 获取sessionId

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 实例 - 弹出框(Popover)插件</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://new.imanager.com/distpicker-master/docs/css/main.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="http://new.imanager.com/imanager/_doc/loading.js"></script>
</head>
<body>

<div class="panel-body" style="padding:0px;background-image:url('../images/rack/jg.gif')"  width="144" height="450">
    <div class="panel panel-default" style="height:450px;">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group">
                    <div class="col-xs-4">
                        <button type="button" id="btn_submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>加载测试</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("#btn_submit").on("click", function () {
            $.ajax({
                type: 'post',
                url: 'http://xxx.zabbix/api_jsonrpc.php',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "jsonrpc": "2.0",
                    "method": "user.login",
                    "params": {
                        "user": "admin",
                        "password": "admin",
                    },
                    "id":1
                }),
                dataType: "json",
                success: function (message) {
                    if (message.result) {
//                        window.location.href = 'http://xxx.zabbix/itmm.php?action=dashboard.view&dashboardid=24&auth='+message.result.sessionid;
//                        window.location.href = 'http://xxx.zabbix/itmm.php?id='+message.result;
                        login(message.result);
                    }
                },
                error: function (message) {
                    alert("alert");
                },
            });
        })
    });
    function login(id) {
        $.ajax({
            url: 'http://xxx.zabbix/index.php?session_id='+id,
            contentType: "application/json;charset=utf-8",
            type: 'get',
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (message) {
                if (message.result) {
                        window.location.href = 'http:/xxx.zabbix/host_screen.php?resourcetype=1&groupid=0&hostid=10126&sid=4476634f98505db4&screenid=0';
//                    window.location.href = 'http://new.imanager.com/imonitor/itmm.php?id='+message.result;
                }
            },
            error: function (message) {
                alert("alert");
            },
        })
    }
</script>

</body>
</html>

zabbix index.php 处理 设置session cookie

$local_url = $_SERVER['QUERY_STRING'];
if (isset($_REQUEST['enter']) && $_REQUEST['enter'] == _('登录')) {/*honglimin modify */
     ******省略
}
else {
// 设置session id  逻辑
    if($local_url){
        $arr = explode('=', $local_url);
        if(count($arr) == 2){
            if(isset($arr[1]) && ($arr[0] == 'session_id')){
                $session_id = $arr['1'];
                CWebUser::setSessionCookie($session_id);         # 将sessionid写在url里,然后从url中获取sessionid,然后再把sessionid写进去
                echo json_encode(array('result'=>'ok'));             # 302跳转
                exit;
            }
        }

    }
    // login the user from the session, if the session id is empty - login as a guest
    CWebUser::checkAuthentication(CWebUser::getSessionCookie());
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
78
粉丝
8
喜欢
43
收藏
49
排名:82
访问:10.4 万
私信
所有博文
社区赞助商