blob: 39c4700be7bd769500c281613d22f8ee40a6077a [file] [log] [blame]
Matteo Scandoloa8bd93e2018-09-13 13:36:50 -07001// Copyright 2018 Open Networking Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Matteo Scandolo189526a2018-07-13 09:10:23 -070015package main
16
17import (
kartikey dubey72ef3b82019-05-27 06:50:04 +000018 "gerrit.opencord.org/kafka-topic-exporter/common/logger"
19 "github.com/Shopify/sarama"
20 "github.com/prometheus/client_golang/prometheus"
Ganesh Bhure0f2449c2019-07-15 10:31:38 +053021 "github.com/prometheus/client_golang/prometheus/promhttp"
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053022 "gopkg.in/yaml.v2"
23 "io/ioutil"
24 "log"
Matteo Scandolo189526a2018-07-13 09:10:23 -070025 "net/http"
kartikey dubey72ef3b82019-05-27 06:50:04 +000026 "strconv"
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053027 "strings"
Matteo Scandoloaab36db2018-10-09 19:54:11 -070028 "sync"
Matteo Scandolo189526a2018-07-13 09:10:23 -070029)
30
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053031func kafkaInit(broker BrokerInfo) {
Matteo Scandolo189526a2018-07-13 09:10:23 -070032 config := sarama.NewConfig()
33 config.Consumer.Return.Errors = true
Matteo Scandoloaab36db2018-10-09 19:54:11 -070034 var wg sync.WaitGroup
35
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053036 master, err := sarama.NewConsumer([]string{broker.Host}, config)
kartikey dubey72ef3b82019-05-27 06:50:04 +000037
Matteo Scandolo189526a2018-07-13 09:10:23 -070038 if err != nil {
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053039 logger.Panic("kafkaInit panic")
Matteo Scandolo189526a2018-07-13 09:10:23 -070040 panic(err)
41 }
42 defer func() {
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053043 logger.Debug("kafkaInit close connection")
Matteo Scandolo189526a2018-07-13 09:10:23 -070044 if err := master.Close(); err != nil {
45 panic(err)
46 }
47 }()
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053048
49 // read topics from config
50 topics := broker.Topics
51
52 // we are spinning threads for each topic, we need to wait for
53 // them to exit before stopping the kafka connection
54 wg.Add(len(topics))
55
56 for _, topic := range topics {
57 t := topic
58 go topicListener(&t, master, wg)
59 }
Matteo Scandolo189526a2018-07-13 09:10:23 -070060
Matteo Scandoloaab36db2018-10-09 19:54:11 -070061 wg.Wait()
Matteo Scandolo189526a2018-07-13 09:10:23 -070062}
63
Ganesh Bhure8d0c9942019-05-24 11:42:09 +053064func runServer(target TargetInfo) {
65 if target.Port == 0 {
66 logger.Warn("Prometheus target port not configured, using default 8080")
67 target.Port = 8080
68 }
69 logger.Debug("Starting HTTP Server on %d port", target.Port)
Ganesh Bhure0f2449c2019-07-15 10:31:38 +053070 http.Handle("/metrics", promhttp.Handler())
71 err := http.ListenAndServe(":"+strconv.Itoa(target.Port), nil)
72 if err != nil {
73 logger.Error("HTTP Server Error: %s", err.Error())
74 }
Matteo Scandolo189526a2018-07-13 09:10:23 -070075}
76
77func init() {
Matteo Scandolo189526a2018-07-13 09:10:23 -070078 // register metrics within Prometheus
Matteo Scandoloaab36db2018-10-09 19:54:11 -070079 prometheus.MustRegister(volthaTxBytesTotal)
80 prometheus.MustRegister(volthaRxBytesTotal)
81 prometheus.MustRegister(volthaTxPacketsTotal)
82 prometheus.MustRegister(volthaRxPacketsTotal)
83 prometheus.MustRegister(volthaTxErrorPacketsTotal)
84 prometheus.MustRegister(volthaRxErrorPacketsTotal)
85
Ganesh Bhure967018e2019-07-29 14:48:32 +053086 prometheus.MustRegister(VolthaOnuLaserBiasCurrent)
87 prometheus.MustRegister(volthaOnuTemperature)
88 prometheus.MustRegister(VolthaOnuPowerFeedVoltage)
89 prometheus.MustRegister(VolthaOnuMeanOpticalLaunchPower)
90 prometheus.MustRegister(VolthaOnuReceivedOpticalPower)
91
Matteo Scandoloaab36db2018-10-09 19:54:11 -070092 prometheus.MustRegister(onosTxBytesTotal)
93 prometheus.MustRegister(onosRxBytesTotal)
94 prometheus.MustRegister(onosTxPacketsTotal)
95 prometheus.MustRegister(onosRxPacketsTotal)
96 prometheus.MustRegister(onosTxDropPacketsTotal)
97 prometheus.MustRegister(onosRxDropPacketsTotal)
kartikey dubey72ef3b82019-05-27 06:50:04 +000098
99 prometheus.MustRegister(onosaaaRxAcceptResponses)
100 prometheus.MustRegister(onosaaaRxRejectResponses)
101 prometheus.MustRegister(onosaaaRxChallengeResponses)
102 prometheus.MustRegister(onosaaaTxAccessRequests)
103 prometheus.MustRegister(onosaaaRxInvalidValidators)
104 prometheus.MustRegister(onosaaaRxUnknownType)
105 prometheus.MustRegister(onosaaaPendingRequests)
106 prometheus.MustRegister(onosaaaRxDroppedResponses)
107 prometheus.MustRegister(onosaaaRxMalformedResponses)
108 prometheus.MustRegister(onosaaaRxUnknownserver)
109 prometheus.MustRegister(onosaaaRequestRttMillis)
110 prometheus.MustRegister(onosaaaRequestReTx)
Daniele Morobe242582019-10-01 14:02:46 -0700111
112 prometheus.MustRegister(onosPppoeUpTermBytes)
113 prometheus.MustRegister(onosPppoeUpTermPackets)
114 prometheus.MustRegister(onosPppoeUpDropBytes)
115 prometheus.MustRegister(onosPppoeUpDropPackets)
116 prometheus.MustRegister(onosPppoeUpControlPackets)
117 prometheus.MustRegister(onosPppoeDownRxBytes)
118 prometheus.MustRegister(onosPppoeDownRxPackets)
119 prometheus.MustRegister(onosPppoeDownTxBytes)
120 prometheus.MustRegister(onosPppoeDownTxPackets)
Ganesh Bhuref15383e2019-11-11 15:15:14 +0530121
122 prometheus.MustRegister(deviceLaserBiasCurrent)
123 prometheus.MustRegister(deviceTemperature)
124 prometheus.MustRegister(deviceTxPower)
125 prometheus.MustRegister(deviceVoltage)
Shubham Sharma57600bf2019-08-09 07:09:57 +0000126
127 prometheus.MustRegister(onosaaaRxEapolLogoff)
128 prometheus.MustRegister(onosaaaTxEapolResIdentityMsg)
129 prometheus.MustRegister(onosaaaTxAuthSuccess)
130 prometheus.MustRegister(onosaaaTxAuthFailure)
131 prometheus.MustRegister(onosaaaTxStartReq)
132 prometheus.MustRegister(onosaaaEapPktTxAuthChooseEap)
133 prometheus.MustRegister(onosaaaTxRespnotNak)
shubham sharmae7f85752019-09-16 13:37:18 +0000134
135 prometheus.MustRegister(onosaaaEapolFramesTx)
136 prometheus.MustRegister(onosaaaAuthStateIdle)
137 prometheus.MustRegister(onosaaaRequestIdFramesTx)
138 prometheus.MustRegister(onosaaaRequestEapFramesTx)
139 prometheus.MustRegister(onosaaaInvalidPktType)
140 prometheus.MustRegister(onosaaaInvalidBodyLength)
141 prometheus.MustRegister(onosaaaValidEapolFramesRx)
142 prometheus.MustRegister(onosaaaPendingResSupplicant)
143 prometheus.MustRegister(onosaaaRxResIdEapFrames)
Matteo Scandolo189526a2018-07-13 09:10:23 -0700144}
145
Ganesh Bhure8d0c9942019-05-24 11:42:09 +0530146func loadConfigFile() Config {
147 m := Config{}
148 // this file path is configmap mounted in pod yaml
149 yamlFile, err := ioutil.ReadFile("/etc/config/conf.yaml")
150 if err != nil {
151 log.Printf("yamlFile.Get err: %v ", err)
152 }
153 err = yaml.Unmarshal(yamlFile, &m)
154 if err != nil {
155 log.Fatalf("Unmarshal: %v", err)
156 }
157 return m
Matteo Scandolo189526a2018-07-13 09:10:23 -0700158}
Ganesh Bhure8d0c9942019-05-24 11:42:09 +0530159
160func main() {
161 // load configuration
162 conf := loadConfigFile()
163
164 // logger setup
165 logger.Setup(conf.Logger.Host, strings.ToUpper(conf.Logger.LogLevel))
166 logger.Info("Connecting to broker: [%s]", conf.Broker.Host)
167
168 go kafkaInit(conf.Broker)
169 runServer(conf.Target)
Ganesh Bhure0f2449c2019-07-15 10:31:38 +0530170}