zabbix / community-templates

Zabbix Community Templates repository
https://share.zabbix.com
MIT License
1.25k stars 1.77k forks source link

Get username and session id of logged in user #328

Open Neelam-zanvar opened 4 months ago

Neelam-zanvar commented 4 months ago

Hi i have created a zabbix frontend module to generate a audit report. For some requirement i want to fetch the current logged in user name for that session i want to fetch it and use it in the php code of zabbix module. How can i get the logged in user name for that session? i tried user.get api but it's not working `function getLoggedInUsername($apiUrl, $apiToken) { $data = [ 'jsonrpc' => '2.0', 'method' => 'user.get', 'params' => [ 'output' => ['userid', 'alias'], 'selectMediatypes' => 'extend', ], 'auth' => $apiToken, 'id' => 1, ];

$options = [
    'http' => [
        'header' => [
            "Content-type: application/json",
            "User-Agent: PHP"
        ],
        'method' => 'POST',
        'content' => json_encode($data),
    ],
];

$context = stream_context_create($options);
$response = file_get_contents($apiUrl, false, $context);

$result = json_decode($response, true);

if (isset($result['result'][0]['alias'])) {
    return $result['result'][0]['alias'];
}

return null;

}

// Zabbix API URL and token $zabbixApiUrl = 'https://zabbix url/zabbix/api_jsonrpc.php'; $zabbixApiToken = 'zabbix token'

// Get the currently logged-in username $loggedInUsername = getLoggedInUsername($zabbixApiUrl, $zabbixApiToken); echo "Retrieved Username (Main Script): $loggedInUsername\n";

` i am getting the following Retrieved Username (Main Script):