<commit_before>#include "libzbxsystemd.h"

// pid that initialised the module, before forking workers.
int mainpid = 0;

// items in this file
static int SYSTEMD_MODVER(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_MANAGER(AGENT_REQUEST *request, AGENT_RESULT *result);
static int SYSTEMD_UNIT(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_UNIT_DISCOVERY(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_SERVICE_INFO(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_SERVICE_DISCOVERY(AGENT_REQUEST*, AGENT_RESULT*);

// items in cgroups.c
int cgroup_init();
int SYSTEMD_CGROUP_CPU(AGENT_REQUEST*, AGENT_RESULT*);
int SYSTEMD_CGROUP_DEV(AGENT_REQUEST*, AGENT_RESULT*);
int SYSTEMD_CGROUP_MEM(AGENT_REQUEST*, AGENT_RESULT*);

ZBX_METRIC *zbx_module_item_list()
{
  static ZBX_METRIC keys[] =
  {
    { "systemd.modver",             0,              SYSTEMD_MODVER,             NULL },
    { "systemd",                    CF_HAVEPARAMS,  SYSTEMD_MANAGER,            "Version" },
    { "systemd.unit",               CF_HAVEPARAMS,  SYSTEMD_UNIT,               "dbus.service,Service,Result" },
    { "systemd.unit.discovery",     CF_HAVEPARAMS,  SYSTEMD_UNIT_DISCOVERY,     NULL },
    { "systemd.service.info",       CF_HAVEPARAMS,  SYSTEMD_SERVICE_INFO,       "dbus.service" },
    { "systemd.service.discovery",  CF_HAVEPARAMS,  SYSTEMD_SERVICE_DISCOVERY,  NULL },
    { "systemd.cgroup.cpu",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_CPU,         "dbus.service,total" },
    { "systemd.cgroup.dev",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_DEV,         "dbus.service,blkio.io_queued,Total" },
    { "systemd.cgroup.mem",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_MEM,         "dbus.service,rss" },
    { NULL }
  };

  return keys;
}

int zbx_module_api_version() {
  return ZBX_MODULE_API_VERSION;
}

int zbx_module_init()
{
    zabbix_log(LOG_LEVEL_DEBUG, LOG_PREFIX "starting v%s, compiled: %s %s", PACKAGE_VERSION, __DATE__, __TIME__);
    mainpid = getpid();
    cgroup_init();
    return ZBX_MODULE_OK;
}

int zbx_module_uninit()
{ 
  dbus_connection_unref(conn);
  return ZBX_MODULE_OK;
}

int timeout = 3000;
void zbx_module_item_timeout(int t)
{
    timeout = t * 1000;
    zabbix_log(LOG_LEVEL_DEBUG, LOG_PREFIX "set timeout to %ims", timeout);
}

static int SYSTEMD_MODVER(AGENT_REQUEST *request, AGENT_RESULT *result)
{
    SET_STR_RESULT(result, strdup(PACKAGE_STRING ", compiled with Zabbix " ZABBIX_VERSION));
    return SYSINFO_RET_OK;
}

// systemd[<property=Version>]
static int SYSTEMD_MANAGER(AGENT_REQUEST *request, AGENT_RESULT *result)
{
  const char      *property;

  if (1 < request->nparam) {
    SET_MSG_RESULT(result, strdup("Invalid number of parameters."));
    return SYSINFO_RET_FAIL;
  }
  
  // resolve property name
  property = get_rparam(request, 0);
  if (NULL == property || '\0' == *property)<commit_msg>Added ref check before dbus_connection_unref. Fixes #17<commit_after>#include "libzbxsystemd.h"

// pid that initialised the module, before forking workers.
int mainpid = 0;

// items in this file
static int SYSTEMD_MODVER(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_MANAGER(AGENT_REQUEST *request, AGENT_RESULT *result);
static int SYSTEMD_UNIT(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_UNIT_DISCOVERY(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_SERVICE_INFO(AGENT_REQUEST*, AGENT_RESULT*);
static int SYSTEMD_SERVICE_DISCOVERY(AGENT_REQUEST*, AGENT_RESULT*);

// items in cgroups.c
int cgroup_init();
int SYSTEMD_CGROUP_CPU(AGENT_REQUEST*, AGENT_RESULT*);
int SYSTEMD_CGROUP_DEV(AGENT_REQUEST*, AGENT_RESULT*);
int SYSTEMD_CGROUP_MEM(AGENT_REQUEST*, AGENT_RESULT*);

ZBX_METRIC *zbx_module_item_list()
{
  static ZBX_METRIC keys[] =
  {
    { "systemd.modver",             0,              SYSTEMD_MODVER,             NULL },
    { "systemd",                    CF_HAVEPARAMS,  SYSTEMD_MANAGER,            "Version" },
    { "systemd.unit",               CF_HAVEPARAMS,  SYSTEMD_UNIT,               "dbus.service,Service,Result" },
    { "systemd.unit.discovery",     CF_HAVEPARAMS,  SYSTEMD_UNIT_DISCOVERY,     NULL },
    { "systemd.service.info",       CF_HAVEPARAMS,  SYSTEMD_SERVICE_INFO,       "dbus.service" },
    { "systemd.service.discovery",  CF_HAVEPARAMS,  SYSTEMD_SERVICE_DISCOVERY,  NULL },
    { "systemd.cgroup.cpu",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_CPU,         "dbus.service,total" },
    { "systemd.cgroup.dev",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_DEV,         "dbus.service,blkio.io_queued,Total" },
    { "systemd.cgroup.mem",         CF_HAVEPARAMS,  SYSTEMD_CGROUP_MEM,         "dbus.service,rss" },
    { NULL }
  };

  return keys;
}

int zbx_module_api_version() {
  return ZBX_MODULE_API_VERSION;
}

int zbx_module_init()
{
    zabbix_log(LOG_LEVEL_DEBUG, LOG_PREFIX "starting v%s, compiled: %s %s", PACKAGE_VERSION, __DATE__, __TIME__);
    mainpid = getpid();
    cgroup_init();
    return ZBX_MODULE_OK;
}

int zbx_module_uninit()
{
  if (NULL != conn)
    dbus_connection_unref(conn);
  return ZBX_MODULE_OK;
}

int timeout = 3000;
void zbx_module_item_timeout(int t)
{
    timeout = t * 1000;
    zabbix_log(LOG_LEVEL_DEBUG, LOG_PREFIX "set timeout to %ims", timeout);
}

static int SYSTEMD_MODVER(AGENT_REQUEST *request, AGENT_RESULT *result)
{
    SET_STR_RESULT(result, strdup(PACKAGE_STRING ", compiled with Zabbix " ZABBIX_VERSION));
    return SYSINFO_RET_OK;
}

// systemd[<property=Version>]
static int SYSTEMD_MANAGER(AGENT_REQUEST *request, AGENT_RESULT *result)
{
  const char      *property;

  if (1 < request->nparam) {
    SET_MSG_RESULT(result, strdup("Invalid number of parameters."));
    return SYSINFO_RET_FAIL;
  }
  
  // resolve property name
  property = get_rparam(request, 0);
  if (NULL == property || '\0' == *property)
{'fraction_non_alphanumeric': 0.11317479604235375, 'fraction_numerical': 0.004860267314702308, 'mean_word_length': 3.9205807002561914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '956244', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2068, 'n_words': 384}