Source
xxxxxxxxxx
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
static zbx_mutex_t vps_lock = ZBX_MUTEX_NULL;
/******************************************************************************
* *
* Purpose: create VPS monitor *
* *
******************************************************************************/
int vps_monitor_create(zbx_vps_monitor_t *monitor, char **error)
{
if (SUCCEED != zbx_mutex_create(&vps_lock, ZBX_MUTEX_VPS_MONITOR, error))
return FAIL;
memset(monitor, 0, sizeof(zbx_vps_monitor_t));
return SUCCEED;
}
/******************************************************************************
* *
* Purpose: destroy VPS monitor *
* *
******************************************************************************/
void vps_monitor_destroy(void)
{
zbx_mutex_destroy(&vps_lock);
}
/******************************************************************************
* *
* Purpose: initialize VPS monitor *
* *
* Comments: This function is called before processes are spawned - *
* no locking is needed. *
* *
******************************************************************************/
void zbx_vps_monitor_init(zbx_uint64_t vps_limit, zbx_uint64_t overcommit_limit)
{
zbx_vps_monitor_t *monitor = &(get_dc_config())->vps_monitor;
monitor->last_flush = time(NULL);
monitor->values_limit = vps_limit * ZBX_VPS_FLUSH_PERIOD;
monitor->overcommit_limit = overcommit_limit;
monitor->overcommit = 0;
}
/******************************************************************************
* *
* Purpose: add number of collected values to the monitor *
* *
******************************************************************************/
void zbx_vps_monitor_add_collected(zbx_uint64_t values_num)
{
zbx_vps_monitor_t *monitor = &(get_dc_config())->vps_monitor;
time_t now;
if (0 == monitor->values_limit)
return;
now = time(NULL);
zbx_mutex_lock(vps_lock);
if (ZBX_VPS_FLUSH_PERIOD <= now - monitor->last_flush)
{
if (monitor->values_limit > monitor->values_num)
{
if (monitor->overcommit > monitor->values_limit - monitor->values_num)
monitor->overcommit -= monitor->values_limit - monitor->values_num;