forked from squeed/prometheus-tor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
124 lines (103 loc) · 2.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/martinohansen/tor_exporter/collector"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/yawning/bulb"
)
var (
listenAddress = flag.String(
"web.listen-address", ":9105",
"Address to listen on for metrics scraping",
)
torControlPort = flag.String(
"tor.control-port", "",
"Address and port of the tor daemon",
)
torControlSocket = flag.String(
"tor.control-socket", "",
"path to the control socket of the tor daemon",
)
torPassword = flag.String(
"tor.password", "",
"password to authenticate to the tor port",
)
)
func main() {
flag.Parse()
err := realMain()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func realMain() error {
if *torControlPort == "" && *torControlSocket == "" {
return errors.New("either --tor.control-port or --tor.control-socket must be passed")
}
// Make a connection just to test
c, err := connect(*torControlSocket, *torControlPort, *torPassword)
if err != nil {
return err
}
c.Close()
exporter := Exporter{
cSocket: *torControlSocket,
cPort: *torControlPort,
password: *torPassword,
}
prometheus.MustRegister(&exporter)
http.Handle("/metrics", prometheus.Handler())
return http.ListenAndServe(*listenAddress, nil)
}
type Exporter struct {
cSocket string
cPort string
password string
isError prometheus.Gauge
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.NumCircuitsDesc
ch <- collector.NumStreamsDesc
ch <- collector.NumOrconnsDesc
ch <- collector.TrafficReadDesc
ch <- collector.TrafficWrittenDesc
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
c, err := connect(e.cSocket, e.cPort, e.password)
if err != nil {
e.isError.Set(1)
return
}
defer c.Close()
if err := collector.ScrapeTraffic(c, ch); err != nil {
log.Errorln(err)
}
if err := collector.ScrapeCircuits(c, ch); err != nil {
log.Errorln(err)
}
}
func connect(cSocket, cPort, password string) (*bulb.Conn, error) {
var c *bulb.Conn
var err error
if cSocket != "" {
c, err = bulb.Dial("unix", cSocket)
} else if cPort != "" {
c, err = bulb.Dial("tcp4", cPort)
} else {
return nil, errors.New("no endpoint specified")
}
if err != nil {
return nil, errors.Wrap(err, "failed to connect to tor")
}
//c.Debug(true)
if err = c.Authenticate(""); err != nil {
return nil, errors.Wrap(err, "Could not authenticate")
}
return c, nil
}