← Programming examples · libnex
01_nodi.c
The file alone · in the libnex-dev package: /usr/share/doc/libnex-dev/examples/
/*
-------------------------------------------------------------------------------------------------
_ ____ _ _____ ___ ___ _ _ ____ _____ ____ _ _ _____ ____ ___ _
/ \ | _ \ / \|_ _/ _ \ |_ _| \ | |/ ___| ____/ ___| \ | | ____| _ \|_ _| / \
/ _ \ | |_) | / _ \ | || | | | | || \| | | _| _|| | _| \| | _| | |_) || | / _ \
/ ___ \| _ < / ___ \| || |_| | | || |\ | |_| | |__| |_| | |\ | |___| _ < | | / ___ \
/_/ \_\_| \_\/_/ \_\_| \___/ |___|_| \_|\____|_____\____|_| \_|_____|_| \_\___/_/ \_\
-------------------------------------------------------------------------------------------------
Copyright (c) 2026 Arato Ingegneria. Tutti i diritti riservati.
Name : 01_nodi.c
Author : Ing. Roberto arato
Version : 1.0.0
Copyright : Ing. Roberto arato (c) 2026
Description : Esempio 1 - collegarsi all'agent, versione della libreria, stato dei nodi e processi
-------------------------------------------------------------------------------------------------
*/
/*
Il primo passo di ogni programma: nex_open apre la connessione all'agent del nodo
locale. Non servono credenziali: l'agent sa dal kernel chi e' il processo. Le risposte
sono JSON (formato in nex.h e nella guida docs/libnex.md) e si liberano con
nex_free. Qui si stampano cosi' come arrivano: un programma vero le legge con una
libreria JSON (json-c, cJSON, jansson...).
Uso: ./01_nodi
*/
#include <stdio.h>
#include <string.h>
#include <nex.h>
int main(void) {
printf("libnex %s\n", nex_version());
nex_conn *nd = nex_open(NULL); // NULL = /run/nex/agent.sock
if (nd == NULL) {
fprintf(stderr, "agent NEX non raggiungibile: nexagentd e' attivo?\n");
return 1;
}
char *json = NULL;
int r = nex_nodes(nd, &json); // tutti i nodi: stato, carico, memoria
if (r != NEX_OK) {
fprintf(stderr, "nodes: %s (%s)\n", nex_strerror(r), json ? json : "");
nex_free(json);
nex_close(nd);
return 1;
}
printf("nodi: %s\n", json);
nex_free(json);
// Processi di tutto il cluster (cluster = 1): i propri, o tutti se si e' root.
if (nex_ps(nd, 1, &json) == NEX_OK) {
printf("processi: %zu byte di JSON\n", strlen(json));
nex_free(json);
}
nex_close(nd);
return 0;
}