summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive-hub/s5hub.go
blob: ba3469711bb59bd949a56539db7906e391e985eb (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// sfive
//
// sfive - spreadspace streaming statistics suite is a generic
// statistic collection tool for streaming server infrastuctures.
// The system collects and stores meta data like number of views
// and throughput from a number of streaming servers and stores
// it in a global data store.
// The data acquisition is designed to be generic and extensible in
// order to support different streaming software.
// sfive also contains tools and applications to filter and visualize
// live and recorded data.
//
//
// Copyright (C) 2014-2017 Christian Pointner <equinox@spreadspace.org>
//                         Markus Grüneis <gimpf@gimpf.org>
//
// This file is part of sfive.
//
// sfive is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3
// as published by the Free Software Foundation.
//
// sfive is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with sfive. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"sync"

	"spreadspace.org/sfive"
)

var s5hl = log.New(os.Stderr, "[s5hub]\t", log.LstdFlags)

func main() {
	// ******************  this should be replaced? by a proper config file

	db := flag.String("db", "/var/lib/sfive/db.bolt", "path to the database file")
	readOnly := flag.Bool("read-only", false, "open database in read-only mode")
	anonymize := flag.Bool("anonymize", false, "anonymize clients IP addresses using crypto-pan")
	anonKeyFile := flag.String("anonymization-key-file", "", "path to the file containing the key to be used for crypto-pan anonymization (default: use randomly generated key)")
	geoipDB := flag.String("geoip-db", "", "path to the Geo-IP database")
	pipe := flag.String("pipe", "/var/run/sfive/pipe", "path to the unix pipe for the pipeserver")
	startPipe := flag.Bool("start-pipe-server", true, "start a connection oriented pipe server; see option pipe")
	pipegram := flag.String("pipegram", "/var/run/sfive/pipegram", "path to the unix datagram pipe for the pipeserver")
	startPipegram := flag.Bool("start-pipegram-server", true, "start a datagram oriented pipe server; see option pipegram")
	web := flag.String("web", ":8000", "port for the webserver to listen on")
	startWeb := flag.Bool("start-web-server", true, "start a webserver")
	forward := flag.String("forward-url", "", "forward to another sfive-server with http server at base-url")
	forwardES := flag.String("forward-es-url", "", "forward to an ElasticSearch *index* via http")
	forwardGraphite := flag.String("forward-graphite", "", "forward to Graphite at this host")
	graphiteBasePath := flag.String("graphite-base-path", "sfive", "use this as base for all paths on graphite")
	forwardPiwik := flag.String("forward-piwik-url", "", "forward to Piwik at this host")
	piwikSiteURL := flag.String("piwik-site-url", "", "use this base url for the site")
	piwikSiteID := flag.Uint("piwik-site-id", 1, "use this site-id for piwik")
	piwikToken := flag.String("piwik-token", "", "the auth token for piwik")
	help := flag.Bool("help", false, "show usage")

	flag.Parse()

	s5hl.Printf("Hello, world.\n")
	if *help {
		flag.Usage()
		return
	}

	cfg := sfive.SrvConfig{}
	if *startPipe {
		cfg.Interfaces.Pipe = sfive.PipeInterfaceConfig{ListenPath: *pipe}
	}
	if *startPipegram {
		cfg.Interfaces.Pipegram = sfive.PipegramInterfaceConfig{ListenPath: *pipegram}
	}
	if *startWeb {
		cfg.Interfaces.Web = sfive.WebInterfaceConfig{ListenAddr: *web}
	}
	cfg.Store = sfive.StoreConfig{*db, *readOnly}
	cfg.Transform = sfive.TransformConfig{*anonymize, *anonKeyFile, *geoipDB}
	cfg.Forwards.SFive = sfive.SFiveForwardConfig{URL: *forward}
	cfg.Forwards.Elasticsearch = sfive.ESForwardConfig{URL: *forwardES}
	cfg.Forwards.Graphite = sfive.GraphiteForwardConfig{Host: *forwardGraphite, BasePath: *graphiteBasePath}
	cfg.Forwards.Piwik = sfive.PiwikForwardConfig{URL: *forwardPiwik, SiteURL: *piwikSiteURL, SiteID: *piwikSiteID}
	cfg.Forwards.Piwik.AuthConfig = sfive.AuthConfig{Token: *piwikToken}

	// ******************  end config

	srv, err := sfive.NewServer(cfg)
	if err != nil {
		s5hl.Fatalf(err.Error())
	}
	defer srv.Close()

	var wg sync.WaitGroup

	if cfg.Interfaces.Pipe.ListenPath != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.ServePipe(cfg.Interfaces.Pipe)
		}()
	}

	if cfg.Interfaces.Pipegram.ListenPath != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.ServePipegram(cfg.Interfaces.Pipegram)
		}()
	}

	if cfg.Interfaces.Web.ListenAddr != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.ServeWeb(cfg.Interfaces.Web)
		}()
	}

	if cfg.Forwards.SFive.URL != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.RunForwarding(cfg.Forwards.SFive)
		}()
	}

	if cfg.Forwards.Elasticsearch.URL != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.RunForwardingEs(cfg.Forwards.Elasticsearch)
		}()
	}

	if cfg.Forwards.Graphite.Host != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.RunForwardingGraphite(cfg.Forwards.Graphite)
		}()
	}

	if cfg.Forwards.Piwik.URL != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			srv.RunForwardingPiwik(cfg.Forwards.Piwik)
		}()
	}

	alldone := make(chan bool)
	go func() {
		defer func() { alldone <- true }()
		wg.Wait()
	}()

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	select {
	case <-c:
		s5hl.Println("received interrupt, shutdown")
		return
	case <-alldone:
		return
	}
}