#!/bin/bash # ================= SCRIPT D'EXEMPLE - LECTURE SEULE ================= # Ce script est identique a celui que vous recevrez, a deux valeurs pres : # VOTRE-PARC et JETON-PERSONNEL-VALABLE-1H (remplaces par les votres). # Il ne s'execute pas : utilisez la commande de votre tableau de bord. echo "Script d'exemple : rien n'a ete installe."; exit 0 # ==================================================================== #!/bin/bash # ================================================================= # Installation automatique Zabbix Agent - Parc : VOTRE-PARC # LogiServ Monitoring | support@logiserv.app | logiserv.app # Zabbix 7.4.16 | macOS (Apple Silicon uniquement) # ================================================================= # COMMENT EXÉCUTER : # sudo bash install_zabbix_macos.sh # ================================================================= # CE QUE FAIT CE SCRIPT (et rien d'autre) : # 1. Telecharge l'agent officiel Zabbix (logiciel open source, AGPLv3) # directement depuis les serveurs de Zabbix : # https://cdn.zabbix.com/zabbix/binaries/stable/7.4/7.4.16/zabbix_agent-7.4.16-macos-arm64-openssl.pkg # 2. Verifie la signature Apple du paquet (Developer ID Installer: # ZABBIX SIA (N4XY2CV2DJ)) et s'arrete si elle est invalide. # 3. Enregistre ce systeme aupres de LogiServ en envoyant UNIQUEMENT : # le nom d'hote et le systeme ("macos"). En retour : une cle de # chiffrement unique (PSK). # 4. Ecrit /usr/local/etc/zabbix/zabbix_agentd.conf (ancienne version # sauvegardee en .bak) et demarre le service launchd officiel. # CE QU'IL NE FAIT PAS : # - n'ouvre aucun port entrant (connexion sortante 10051/TCP, TLS-PSK) ; # - n'autorise que 4 commandes de lecture (iostat, ps, netstat : CPU, # processus, connexions) ; toute autre commande distante est refusee ; # - ne lit pas vos fichiers. # DESINSTALLER A TOUT MOMENT : # curl -sSL "https://www.logiserv.app/api/agent-uninstall/macos" | sudo bash # Detail de ce que collecte l'agent : https://www.logiserv.app/confiance # ================================================================= set -e RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; GRAY='\033[0;90m'; NC='\033[0m' START_TS=$(date +%s) [ "$EUID" -ne 0 ] && { echo -e "${RED}Erreur : sudo requis. Utilisez : sudo bash $0${NC}"; exit 1; } echo "" echo -e "${CYAN}===================================================================${NC}" echo -e "${CYAN} Installation Zabbix Agent - Parc : VOTRE-PARC${NC}" echo -e "${CYAN}===================================================================${NC}" echo " Systeme : $(hostname)" echo " Serveur : zabbix.logiserv.re" echo -e "${GRAY} Deroulement (5 etapes) : 1.telechargement 2.installation${NC}" echo -e "${GRAY} 3.enrolement 4.config 5.service${NC}" echo -e "${GRAY} Ne fermez pas la fenetre pendant le telechargement/installation.${NC}" echo -e "${CYAN}===================================================================${NC}" echo "" CONF_FILE="/usr/local/etc/zabbix/zabbix_agentd.conf" PSK_FILE="/usr/local/etc/zabbix/zabbix_agentd.psk" ENROLL_URL="https://www.logiserv.app/api/agent-enroll" ENROLL_TOKEN="JETON-PERSONNEL-VALABLE-1H" # -- Helpers : POST HTTP (curl) + extraction JSON (sans jq) -- http_post() { curl -sS -X POST "$1" -H "Content-Type: application/json" -d "$2" } json_get() { printf '%s' "$1" | tr ',' '\n' | grep -m1 "\"$2\"" | sed -e 's/.*:[[:space:]]*"//' -e 's/".*//' } # -- Architecture -- # uname -m renvoie x86_64 sous Rosetta : sysctl donne la vraie puce. ARCH=$(uname -m) [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" = "1" ] && ARCH=arm64 if [ "$ARCH" != "arm64" ]; then echo "" echo -e "${RED}ERREUR : Mac Intel (x86_64) non supporte.${NC}" echo -e "${YELLOW}Zabbix ne publie l'agent macOS que pour Apple Silicon (M1 et suivants).${NC}" echo "Contactez support@logiserv.app pour une solution alternative." exit 1 fi PKG_URL="https://cdn.zabbix.com/zabbix/binaries/stable/7.4/7.4.16/zabbix_agent-7.4.16-macos-arm64-openssl.pkg" echo "Architecture : Apple Silicon (arm64)" # -- [1/5] Téléchargement -- echo "" echo -e "${CYAN}[1/5] Telechargement de l'agent Zabbix 7.4.16...${NC}" curl -fL --progress-bar "$PKG_URL" -o /tmp/zabbix_agent.pkg echo -e "${GREEN} OK - Package telecharge.${NC}" # Verification de la signature Apple (Developer ID de Zabbix SIA) : # un paquet altere ou signe par un tiers n'est jamais installe. SIG_OUT=$(pkgutil --check-signature /tmp/zabbix_agent.pkg 2>&1 || true) if ! printf '%s' "$SIG_OUT" | grep -q "Status: signed" || \ ! printf '%s' "$SIG_OUT" | grep -q "Developer ID Installer: ZABBIX SIA (N4XY2CV2DJ)"; then rm -f /tmp/zabbix_agent.pkg echo -e "${RED}ERREUR : signature du paquet Zabbix invalide. Installation annulee.${NC}" echo "$SIG_OUT" echo "Contactez support@logiserv.app." exit 1 fi echo -e "${GREEN} OK - Signature verifiee : ZABBIX SIA (N4XY2CV2DJ).${NC}" # -- [2/5] Installation -- echo "" echo -e "${CYAN}[2/5] Installation du package...${NC}" installer -pkg /tmp/zabbix_agent.pkg -target / echo -e "${GREEN} OK - Agent installe.${NC}" # -- [3/5] Enrôlement auprès de LogiServ -- echo "" echo -e "${CYAN}[3/5] Enrolement du systeme aupres de LogiServ...${NC}" mkdir -p /usr/local/etc/zabbix MACHINE_HOSTNAME=$(hostname) # Portable (batterie interne) : alertes de disponibilite assouplies cote LogiServ (mise en veille). IS_WORKSTATION=false if pmset -g batt 2>/dev/null | grep -q "InternalBattery"; then IS_WORKSTATION=true fi echo -e "${GRAY} Donnees envoyees : nom=$MACHINE_HOSTNAME, systeme=macos, portable=$IS_WORKSTATION${NC}" ENROLL_BODY=$(printf '{"token":"%s","hostname":"%s","os":"macos","rotateExisting":true,"isWorkstation":%s}' "$ENROLL_TOKEN" "$MACHINE_HOSTNAME" "$IS_WORKSTATION") ENROLL_RESP=$(http_post "$ENROLL_URL" "$ENROLL_BODY") || { echo -e "${RED}ERREUR : échec de l'enrôlement auprès de LogiServ.${NC}" echo "URL : $ENROLL_URL" exit 1 } # Vérifier si la réponse contient une erreur ERROR_MSG=$(json_get "$ENROLL_RESP" "error") if [ -n "$ERROR_MSG" ]; then echo "" echo -e "${RED}ERREUR D'ENRÔLEMENT${NC}" echo -e "${YELLOW}$ERROR_MSG${NC}" echo "" echo "Pour plus d'informations, consultez votre compte sur le portail LogiServ." echo "" exit 1 fi E_PSK=$(json_get "$ENROLL_RESP" "psk") E_HOSTNAME=$(json_get "$ENROLL_RESP" "hostname") E_IDENTITY=$(json_get "$ENROLL_RESP" "pskIdentity") E_METADATA=$(json_get "$ENROLL_RESP" "hostMetadata") E_SERVER=$(json_get "$ENROLL_RESP" "serverActive") if [ -z "$E_PSK" ] || [ -z "$E_SERVER" ]; then echo -e "${RED}ERREUR : réponse d'enrôlement invalide.${NC}" echo "Réponse : $ENROLL_RESP" exit 1 fi echo -e "${GREEN} OK - Hote enrole : $E_HOSTNAME${NC}" # -- [4/5] Configuration -- echo "" echo -e "${CYAN}[4/5] Ecriture de la configuration...${NC}" [ -f "$CONF_FILE" ] && cp "$CONF_FILE" "${CONF_FILE}.bak.$(date +%Y%m%d%H%M%S)" || true # launchd lance l'agent sous l'utilisateur zabbix (UserName du plist officiel) : # journal dans /var/log/zabbix (cree et attribue a zabbix par le preinstall du # .pkg), PidFile laisse a sa valeur compilee (/tmp), cle PSK lisible par zabbix. mkdir -p /var/log/zabbix chown zabbix:zabbix /var/log/zabbix 2>/dev/null || true cat > "$CONF_FILE" << ZABBIX_CONF LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Hostname=$E_HOSTNAME TLSConnect=psk TLSAccept=psk TLSPSKFile=$PSK_FILE TLSPSKIdentity=$E_IDENTITY HostMetadata=$E_METADATA # Mode actif uniquement : verifications passives desactivees, aucun port en ecoute. StartAgents=0 ServerActive=$E_SERVER # Seules ces commandes de lecture (CPU, processus, connexions : cles absentes # de l'agent macOS) peuvent etre executees ; toute autre commande est refusee. AllowKey=system.run["iostat -n0 -c2 -w1"] AllowKey=system.run["ps -A -o pid="] AllowKey=system.run["ps -U root -o stat="] AllowKey=system.run["netstat -an -p tcp"] AllowKey=system.run["system_profiler -json SPHardwareDataType"] AllowKey=system.run["system_profiler -json SPStorageDataType"] AllowKey=system.run["system_profiler -json SPNetworkDataType"] DenyKey=system.run[*] ZABBIX_CONF printf '%s' "$E_PSK" > "$PSK_FILE" chmod 600 "$PSK_FILE" chown zabbix:zabbix "$PSK_FILE" chmod 644 "$CONF_FILE" # -- [5/5] Service launchd -- echo "" echo -e "${CYAN}[5/5] Demarrage du service...${NC}" PLIST="" [ -f "/Library/LaunchDaemons/com.zabbix.zabbix_agentd.plist" ] && PLIST="/Library/LaunchDaemons/com.zabbix.zabbix_agentd.plist" [ -f "/Library/LaunchDaemons/org.zabbix.zabbix_agentd.plist" ] && PLIST="/Library/LaunchDaemons/org.zabbix.zabbix_agentd.plist" if [ -n "$PLIST" ]; then launchctl unload "$PLIST" 2>/dev/null || true launchctl load "$PLIST" fi sleep 2 # Application « Désinstaller Logiserv » dans /Applications (deja deposee par l'installateur en # un clic) : signee et notarisee, verifiee avant d'etre gardee. Facultative : un echec ici ne # compromet pas l'agent, la desinstallation reste possible en commande. UNINSTALLER_APP="/Applications/Désinstaller Logiserv.app" if [ ! -d "$UNINSTALLER_APP" ]; then UNINSTALLER_ZIP=$(mktemp /tmp/logiserv-desinstaller.XXXXXX) if curl -fsSL --max-time 60 -o "$UNINSTALLER_ZIP" "https://www.logiserv.app/api/agent-uninstall/macos?format=app" \ && ditto -x -k "$UNINSTALLER_ZIP" /Applications \ && codesign --verify --strict "$UNINSTALLER_APP" 2>/dev/null \ && spctl --assess --type execute "$UNINSTALLER_APP" 2>/dev/null; then echo -e "${GRAY} Desinstalleur : /Applications/Désinstaller Logiserv.app${NC}" else rm -rf "$UNINSTALLER_APP" fi rm -f "$UNINSTALLER_ZIP" fi # Enregistre l'adresse logiserv-desinstaller:// (bouton de la page Agents) aupres de macOS # pour l'utilisateur connecte. CONSOLE_USER=$(stat -f%Su /dev/console) if [ -d "$UNINSTALLER_APP" ] && [ -n "$CONSOLE_USER" ] && [ "$CONSOLE_USER" != "root" ]; then sudo -u "$CONSOLE_USER" /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$UNINSTALLER_APP" 2>/dev/null || true fi DUR=$(( $(date +%s) - START_TS )) echo "" echo -e "${GREEN}===================================================================${NC}" if [ -n "$PLIST" ] && launchctl list 2>/dev/null | grep -q zabbix; then echo -e "${GREEN} Installation terminee avec succes en ${DUR}s !${NC}" echo -e "${GREEN} Service zabbix_agentd : charge (running)${NC}" else echo -e "${YELLOW} Installation terminee en ${DUR}s.${NC}" echo -e "${YELLOW} Service : plist launchd non trouve/charge - demarrez manuellement.${NC}" fi echo -e "${GRAY}-------------------------------------------------------------------${NC}" echo " Hote : $E_HOSTNAME" echo " Serveur : zabbix.logiserv.re" echo " Config : $CONF_FILE" echo " Logs : tail -f /var/log/zabbix/zabbix_agentd.log" echo -e "${GREEN}===================================================================${NC}"