2018-09-05 03:08:48 +02:00
package main
import (
2018-09-06 01:06:16 +02:00
"flag"
2018-09-05 03:08:48 +02:00
"fmt"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
2021-02-28 11:44:31 +01:00
"github.com/trazfr/freebox-exporter/fbx"
2018-09-05 03:08:48 +02:00
"github.com/trazfr/freebox-exporter/log"
)
2021-02-28 13:28:12 +01:00
func usage ( ) {
fmt . Fprintf ( flag . CommandLine . Output ( ) ,
"Usage: %s [options] <api_token_file>\n" +
"\n" +
"api_token_file: file to store the token for the API\n" +
"\n" +
"options:\n" ,
os . Args [ 0 ] )
2018-09-06 01:06:16 +02:00
flag . PrintDefaults ( )
}
2018-09-05 03:08:48 +02:00
func main ( ) {
2021-02-28 13:28:12 +01:00
flag . Usage = usage
2018-09-06 01:06:16 +02:00
debugPtr := flag . Bool ( "debug" , false , "enable the debug mode" )
2021-02-27 11:15:08 +01:00
hostDetailsPtr := flag . Bool ( "hostDetails" , false , "get details about the hosts connected to wifi and ethernet. This increases the number of metrics" )
2021-02-28 13:28:12 +01:00
httpDiscoveryPtr := flag . Bool ( "httpDiscovery" , false , "use http://mafreebox.freebox.fr/api_version to discover the Freebox at the first run (by default: use mDNS)" )
2022-09-30 22:08:14 +02:00
apiVersionPtr := flag . Int ( "apiVersion" , 0 , "Force the API version (by default use the latest one)" )
2021-02-26 08:15:12 +01:00
listenPtr := flag . String ( "listen" , ":9091" , "listen to address" )
2018-09-06 01:06:16 +02:00
flag . Parse ( )
args := flag . Args ( )
if len ( args ) < 1 {
2021-02-28 13:28:12 +01:00
fmt . Fprintf ( flag . CommandLine . Output ( ) , "ERROR: api_token_file not defined\n" )
usage ( )
os . Exit ( 1 )
2018-09-06 01:06:16 +02:00
} else if len ( args ) > 1 {
2021-02-28 13:28:12 +01:00
fmt . Fprintf ( flag . CommandLine . Output ( ) , "ERROR: too many arguments\n" )
usage ( )
os . Exit ( 1 )
2018-09-05 03:08:48 +02:00
}
2018-09-06 01:06:16 +02:00
if * debugPtr {
2021-02-28 13:28:12 +01:00
log . InitDebug ( )
2018-09-06 01:06:16 +02:00
} else {
2021-02-28 13:28:12 +01:00
log . Init ( )
2018-09-05 03:08:48 +02:00
}
2021-02-28 11:44:31 +01:00
discovery := fbx . FreeboxDiscoveryMDNS
if * httpDiscoveryPtr {
discovery = fbx . FreeboxDiscoveryHTTP
}
2022-09-30 22:08:14 +02:00
collector := NewCollector ( args [ 0 ] , discovery , * apiVersionPtr , * hostDetailsPtr , * debugPtr )
2021-02-26 08:15:12 +01:00
defer collector . Close ( )
2018-09-05 03:08:48 +02:00
2021-02-26 08:15:12 +01:00
prometheus . MustRegister ( collector )
2018-09-05 03:08:48 +02:00
http . Handle ( "/metrics" , promhttp . Handler ( ) )
2021-02-28 13:28:12 +01:00
log . Info . Println ( "Listen to" , * listenPtr )
2021-02-26 08:15:12 +01:00
log . Error . Println ( http . ListenAndServe ( * listenPtr , nil ) )
2018-09-05 03:08:48 +02:00
}