#!/usr/bin/env bash
set -euo pipefail

# Deploy Smokeping on tsys-librenms
# Part of OAM platform [#337]

echo "=== Setting up Smokeping ==="

# Ensure directories
mkdir -p /opt/oam/smokeping/{config,data}

# General config
cat > /opt/oam/smokeping/config/General <<'GENEOF'
*** General ***

owner = Charles N Wyble
contact = reachableceo@knownelement.com
mailhost = localhost
cgiurl = http://tsys-librenms.knel.net:8081/smokeping/smokeping.cgi
syslogfacility = local0
pagedir = /data/pages
piddir  = /tmp
imgcache = /data/cache
imgurl  = /smokeping/images
datadir = /data/_data
dyndir = /data/dyn

*** Database ***

step     = 300
pings    = 20

AVERAGE  0.5   1  100800
AVERAGE  0.5  12  43200
    MIN  0.5  12  43200
    MAX  0.5  12  43200
AVERAGE  0.5 144   7200
    MAX  0.5 144   7200
    MIN  0.5 144   7200

*** Presentation ***

template = /etc/smokeping/basepage.html

+ charts

+ detail
width = 600
height = 200
nradius = 5

*** Probes ***

+ FPing

binary = /usr/local/bin/fping
hostinterval = 1.5
mininterval = 0.001
offset = 0%

*** Alerts ***

to = reachableceo@knownelement.com
from = smokeping-alert@tsys-librenms.knel.net

+bigloss
type = loss
pattern = ==0%,==0%,==0%,==0%,>0%,>0%,>0%
comment = suddenly there is packet loss

+somedoss
type = loss
pattern = ==0%,==0%,==0%,==0%,==0%,>0%,>0%,>0%,>0%,>0%,>0%,>0%
comment = incoming packet loss

+startloss
type = loss
pattern = >0%,>0%,>0%
comment = loss at startup

+rttdetect
type = rtt
pattern = <10,<10,<10,<10,<10,<10,<10,>10,>10,>10
comment = routing change?

+rttbadd
type = rtt
pattern = <50,<50,<50,<50,<50,<50,>50,>50,>50,>50
comment = reroute?
GENEOF

# Targets config
cat > /opt/oam/smokeping/config/Targets <<'TGTEOF'
*** Targets ***

probe = FPing

menu = Top
title = Network Latency Monitor

+ Infrastructure
menu = Infrastructure
title = Core Infrastructure

++ pfv-r5-core-01
menu = pfv-r5-core-01 (Core Switch)
title = Dell PowerConnect 5448 - Rack 5 Core
host = pfv-r5-core-01.knel.net

++ pfv-r3-tor-mgmt-01
menu = pfv-r3-tor-mgmt-01 (Rack 3 Mgmt TOR)
title = Neyland 24T - Rack 3 Management TOR
host = pfv-r3-tor-mgmt-01.knel.net

++ pfv-r3-tor-stor-01
menu = pfv-r3-tor-stor-01 (Rack 3 Storage TOR)
title = Neyland 24T - Rack 3 Storage TOR
host = pfv-r3-tor-stor-01.knel.net

++ pfv-r6-mgmt-01
menu = pfv-r6-mgmt-01 (Rack 6 Mgmt Switch)
title = Neyland 24T - Rack 6 Management Switch
host = pfv-r6-mgmt-01.knel.net

+ Servers
menu = Servers
title = Server Infrastructure

++ pfv-stor1
menu = pfv-stor1
title = Storage Server 1
host = pfv-stor1.knel.net

++ pfv-bms
menu = pfv-bms (HomeAssistant)
title = Home Assistant / UniFi Controller
host = pfv-bms.knel.net

++ tsys-librenms
menu = tsys-librenms
title = LibreNMS Monitoring Server
host = tsys-librenms.knel.net

+ Wireless
menu = Wireless
title = Wireless Access Points

++ ap-tablemount
menu = ap-tablemount
title = UniFi AP-AC-Lite (Table Mount)
host = ap-tablemount.knel.net

++ ap-wallmount
menu = ap-wallmount
title = UniFi AP-AC-LR (Wall Mount)
host = ap-wallmount.knel.net

+ ProxmoxHosts
menu = Proxmox Hosts
title = Proxmox Hypervisor Nodes

++ pfv-tsys1
menu = pfv-tsys1
title = Proxmox Host - Infrastructure
host = pfv-tsys1.knel.net

++ pfv-tsys3
menu = pfv-tsys3
title = Proxmox Host - Compute
host = pfv-tsys3.knel.net

++ pfv-tsys4
menu = pfv-tsys4
title = Proxmox Host - Storage + PBS
host = pfv-tsys4.knel.net

++ pfv-tsys5
menu = pfv-tsys5
title = Proxmox Host - Storage + Sandbox
host = pfv-tsys5.knel.net

++ pfv-tsys6
menu = pfv-tsys6
title = Proxmox Host - RackRental
host = pfv-tsys6.knel.net

++ pfv-tsys7
menu = pfv-tsys7
title = Proxmox Host - RackRental
host = pfv-tsys7.knel.net

++ pfv-tsys9
menu = pfv-tsys9
title = Proxmox Host - Infra + Compute
host = pfv-tsys9.knel.net

+ Power
menu = Power
title = Power Devices

++ pfv-garage-pdu-01
menu = pfv-garage-pdu-1
title = APC PDU (Garage)
host = pfv-garage-pdu-1.knel.net
TGTEOF

# Docker compose
cat > /opt/oam/smokeping/docker-compose.yml <<'DCEOF'
services:
  smokeping:
    image: linuxserver/smokeping:2.9.0
    container_name: oam-smokeping
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Chicago
    volumes:
      - /opt/oam/smokeping/config:/config
      - /opt/oam/smokeping/data:/data
    ports:
      - "8081:80"
    restart: unless-stopped
DCEOF

echo "=== Starting Smokeping container ==="
cd /opt/oam/smokeping && docker compose up -d 2>&1

echo "=== Waiting for startup ==="
sleep 5

echo "=== Container status ==="
docker ps --filter name=oam-smokeping --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

echo "=== Smokeping logs (last 20) ==="
docker logs oam-smokeping --tail 20 2>&1

echo "=== Done ==="
