Matteo Scandolo | a8bd93e | 2018-09-13 13:36:50 -0700 | [diff] [blame] | 1 | // 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 Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 15 | package main |
| 16 | |
| 17 | import ( |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 18 | "gerrit.opencord.org/kafka-topic-exporter/common/logger" |
| 19 | "github.com/Shopify/sarama" |
| 20 | "github.com/prometheus/client_golang/prometheus" |
Ganesh Bhure | 0f2449c | 2019-07-15 10:31:38 +0530 | [diff] [blame] | 21 | "github.com/prometheus/client_golang/prometheus/promhttp" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 22 | "gopkg.in/yaml.v2" |
| 23 | "io/ioutil" |
| 24 | "log" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 25 | "net/http" |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 26 | "strconv" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 27 | "strings" |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 28 | "sync" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 31 | func kafkaInit(broker BrokerInfo) { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 32 | config := sarama.NewConfig() |
| 33 | config.Consumer.Return.Errors = true |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 34 | var wg sync.WaitGroup |
| 35 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 36 | master, err := sarama.NewConsumer([]string{broker.Host}, config) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 37 | |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 38 | if err != nil { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 39 | logger.Panic("kafkaInit panic") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 40 | panic(err) |
| 41 | } |
| 42 | defer func() { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 43 | logger.Debug("kafkaInit close connection") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 44 | if err := master.Close(); err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | }() |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 48 | |
| 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 Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 61 | wg.Wait() |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 64 | func 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 Bhure | 0f2449c | 2019-07-15 10:31:38 +0530 | [diff] [blame] | 70 | 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 Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | func init() { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 78 | // register metrics within Prometheus |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 79 | 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 Bhure | 967018e | 2019-07-29 14:48:32 +0530 | [diff] [blame] | 86 | prometheus.MustRegister(VolthaOnuLaserBiasCurrent) |
| 87 | prometheus.MustRegister(volthaOnuTemperature) |
| 88 | prometheus.MustRegister(VolthaOnuPowerFeedVoltage) |
| 89 | prometheus.MustRegister(VolthaOnuMeanOpticalLaunchPower) |
| 90 | prometheus.MustRegister(VolthaOnuReceivedOpticalPower) |
| 91 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 92 | prometheus.MustRegister(onosTxBytesTotal) |
| 93 | prometheus.MustRegister(onosRxBytesTotal) |
| 94 | prometheus.MustRegister(onosTxPacketsTotal) |
| 95 | prometheus.MustRegister(onosRxPacketsTotal) |
| 96 | prometheus.MustRegister(onosTxDropPacketsTotal) |
| 97 | prometheus.MustRegister(onosRxDropPacketsTotal) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 98 | |
| 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 Moro | be24258 | 2019-10-01 14:02:46 -0700 | [diff] [blame] | 111 | |
| 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 Bhure | f15383e | 2019-11-11 15:15:14 +0530 | [diff] [blame] | 121 | |
| 122 | prometheus.MustRegister(deviceLaserBiasCurrent) |
| 123 | prometheus.MustRegister(deviceTemperature) |
| 124 | prometheus.MustRegister(deviceTxPower) |
| 125 | prometheus.MustRegister(deviceVoltage) |
Shubham Sharma | 57600bf | 2019-08-09 07:09:57 +0000 | [diff] [blame] | 126 | |
| 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 sharma | e7f8575 | 2019-09-16 13:37:18 +0000 | [diff] [blame^] | 134 | |
| 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 Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 146 | func 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 Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 158 | } |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 159 | |
| 160 | func 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 Bhure | 0f2449c | 2019-07-15 10:31:38 +0530 | [diff] [blame] | 170 | } |