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 ( |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "log" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 20 | "net/http" |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 21 | "strconv" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 22 | "strings" |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 23 | "sync" |
kesavand | 2cde658 | 2020-06-22 04:56:23 -0400 | [diff] [blame] | 24 | |
| 25 | "gerrit.opencord.org/kafka-topic-exporter/common/logger" |
| 26 | "github.com/Shopify/sarama" |
| 27 | "github.com/prometheus/client_golang/prometheus" |
| 28 | "github.com/prometheus/client_golang/prometheus/promhttp" |
kesavand | 0c06492 | 2020-12-15 15:36:25 +0530 | [diff] [blame^] | 29 | "gerrit.opencord.org/kafka-topic-exporter/utils" |
kesavand | 2cde658 | 2020-06-22 04:56:23 -0400 | [diff] [blame] | 30 | "gopkg.in/yaml.v2" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 33 | func kafkaInit(broker BrokerInfo) { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 34 | config := sarama.NewConfig() |
| 35 | config.Consumer.Return.Errors = true |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 36 | var wg sync.WaitGroup |
| 37 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 38 | master, err := sarama.NewConsumer([]string{broker.Host}, config) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 39 | |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 40 | if err != nil { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 41 | logger.Panic("kafkaInit panic") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 42 | panic(err) |
| 43 | } |
| 44 | defer func() { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 45 | logger.Debug("kafkaInit close connection") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 46 | if err := master.Close(); err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | }() |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 50 | |
| 51 | // read topics from config |
| 52 | topics := broker.Topics |
| 53 | |
| 54 | // we are spinning threads for each topic, we need to wait for |
| 55 | // them to exit before stopping the kafka connection |
| 56 | wg.Add(len(topics)) |
| 57 | |
| 58 | for _, topic := range topics { |
| 59 | t := topic |
| 60 | go topicListener(&t, master, wg) |
| 61 | } |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 62 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 63 | wg.Wait() |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 66 | func runServer(target TargetInfo) { |
| 67 | if target.Port == 0 { |
| 68 | logger.Warn("Prometheus target port not configured, using default 8080") |
| 69 | target.Port = 8080 |
| 70 | } |
| 71 | logger.Debug("Starting HTTP Server on %d port", target.Port) |
Ganesh Bhure | 0f2449c | 2019-07-15 10:31:38 +0530 | [diff] [blame] | 72 | http.Handle("/metrics", promhttp.Handler()) |
| 73 | err := http.ListenAndServe(":"+strconv.Itoa(target.Port), nil) |
| 74 | if err != nil { |
| 75 | logger.Error("HTTP Server Error: %s", err.Error()) |
kesavand | 6753e5e | 2020-04-28 03:34:21 -0400 | [diff] [blame] | 76 | } |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func init() { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 80 | // register metrics within Prometheus |
kesavand | 6753e5e | 2020-04-28 03:34:21 -0400 | [diff] [blame] | 81 | prometheus.MustRegister(volthaOltTxBytesTotal) |
| 82 | prometheus.MustRegister(volthaOltRxBytesTotal) |
| 83 | prometheus.MustRegister(volthaOltTxPacketsTotal) |
| 84 | prometheus.MustRegister(volthaOltRxPacketsTotal) |
| 85 | prometheus.MustRegister(volthaOltTxErrorPacketsTotal) |
| 86 | prometheus.MustRegister(volthaOltRxErrorPacketsTotal) |
| 87 | prometheus.MustRegister(volthaOltTxBroadcastPacketsTotal) |
| 88 | prometheus.MustRegister(volthaOltTxUnicastPacketsTotal) |
| 89 | prometheus.MustRegister(volthaOltTxMulticastPacketsTotal) |
| 90 | prometheus.MustRegister(volthaOltRxBroadcastPacketsTotal) |
| 91 | prometheus.MustRegister(volthaOltRxUnicastPacketsTotal) |
| 92 | prometheus.MustRegister(volthaOltRxMulticastPacketsTotal) |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 93 | |
Ganesh Bhure | 967018e | 2019-07-29 14:48:32 +0530 | [diff] [blame] | 94 | prometheus.MustRegister(VolthaOnuLaserBiasCurrent) |
| 95 | prometheus.MustRegister(volthaOnuTemperature) |
| 96 | prometheus.MustRegister(VolthaOnuPowerFeedVoltage) |
| 97 | prometheus.MustRegister(VolthaOnuMeanOpticalLaunchPower) |
| 98 | prometheus.MustRegister(VolthaOnuReceivedOpticalPower) |
kesavand | 6753e5e | 2020-04-28 03:34:21 -0400 | [diff] [blame] | 99 | prometheus.MustRegister(VolthaOnuTransmtOpticalPower) |
| 100 | |
| 101 | prometheus.MustRegister(volthaOnuFecCorrectedCodewordsTotal) |
| 102 | prometheus.MustRegister(volthaOnuFecCodewordsTotal) |
| 103 | prometheus.MustRegister(volthaOnuFecCorrectedBytesTotal) |
| 104 | prometheus.MustRegister(volthaOnuFecSecondsTotal) |
| 105 | prometheus.MustRegister(volthaOnuFecUncorrectablewordsTotal) |
| 106 | |
| 107 | prometheus.MustRegister(volthaEthernetUniSingleCollisionTotal) |
| 108 | prometheus.MustRegister(volthaEthernetUniMacLayerTramsmitErrorTotal) |
| 109 | prometheus.MustRegister(volthaEthernetUniMultiCollisionTotal) |
| 110 | prometheus.MustRegister(volthaEthernetUniFramestooLongTotal) |
| 111 | prometheus.MustRegister(volthaEthernetUniAlignmentErrorTotal) |
| 112 | prometheus.MustRegister(volthaEthernetUniCarrierErrorTotal) |
| 113 | prometheus.MustRegister(volthaEthernetUniExcessiveCollisionErrorTotal) |
| 114 | prometheus.MustRegister(volthaEthernetUniDeferredTxTotal) |
| 115 | prometheus.MustRegister(volthaEthernetUniLateCollisionTotal) |
| 116 | prometheus.MustRegister(volthaEthernetUniBufferOverflowsRxErrorTotal) |
| 117 | prometheus.MustRegister(volthaEthernetUniFcsErrorTotal) |
| 118 | prometheus.MustRegister(volthaEthernetUniSqeErrorTotal) |
| 119 | prometheus.MustRegister(volthaEthernetUniBufferOverflowsTxErrorTotal) |
| 120 | |
| 121 | prometheus.MustRegister(volthaOnuBridgePortRxBytesTotal) |
| 122 | prometheus.MustRegister(volthaOnuBridgePortRxPacketsTotal) |
| 123 | prometheus.MustRegister(volthaOnuBridgePort_64octetRxPacketsTotal) |
| 124 | prometheus.MustRegister(volthaOnuBridgePort_65_127_octetRxPacketsTotal) |
| 125 | prometheus.MustRegister(volthaOnuBridgePort_128_255_octetRxPacketsTotal) |
| 126 | prometheus.MustRegister(volthaOnuBridgePort_256_511_octetRxPacketsTotal) |
| 127 | prometheus.MustRegister(volthaOnuBridgePort_512_1023_octetRxPacketsTotal) |
| 128 | prometheus.MustRegister(volthaOnuBridgePort_1024_1518_octetRxPacketsTotal) |
| 129 | prometheus.MustRegister(volthaOnuBridgePortRxMulticastPacketsTotal) |
| 130 | prometheus.MustRegister(volthaOnuBridgePortRxBroadcastPacketsTotal) |
| 131 | prometheus.MustRegister(volthaOnuBridgePortRxOversizePacketsTotal) |
| 132 | prometheus.MustRegister(volthaOnuBridgePortRxCrcErrorPacketsTotal) |
| 133 | prometheus.MustRegister(volthaOnuBridgePortRxUndersizePacketsTotal) |
| 134 | prometheus.MustRegister(volthaOnuBridgePortRxDropEventsTotal) |
| 135 | |
| 136 | prometheus.MustRegister(volthaOnuBridgePortTxBytesTotal) |
| 137 | prometheus.MustRegister(volthaOnuBridgePortTxPacketsTotal) |
| 138 | prometheus.MustRegister(volthaOnuBridgePort_64octetTxPacketsTotal) |
| 139 | prometheus.MustRegister(volthaOnuBridgePort_65_127_octetTxPacketsTotal) |
| 140 | prometheus.MustRegister(volthaOnuBridgePort_128_255_octetTxPacketsTotal) |
| 141 | prometheus.MustRegister(volthaOnuBridgePort_256_511_octetTxPacketsTotal) |
| 142 | prometheus.MustRegister(volthaOnuBridgePort_512_1023_octetTxPacketsTotal) |
| 143 | prometheus.MustRegister(volthaOnuBridgePort_1024_1518_octetTxPacketsTotal) |
| 144 | prometheus.MustRegister(volthaOnuBridgePortTxMulticastPacketsTotal) |
| 145 | prometheus.MustRegister(volthaOnuBridgePortTxBroadcastPacketsTotal) |
| 146 | prometheus.MustRegister(volthaOnuBridgePortTxOversizePacketsTotal) |
| 147 | prometheus.MustRegister(volthaOnuBridgePortTxCrcErrorPacketsTotal) |
| 148 | prometheus.MustRegister(volthaOnuBridgePortTxUndersizePacketsTotal) |
| 149 | prometheus.MustRegister(volthaOnuBridgePortTxDropEventsTotal) |
Ganesh Bhure | 967018e | 2019-07-29 14:48:32 +0530 | [diff] [blame] | 150 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 151 | prometheus.MustRegister(onosTxBytesTotal) |
| 152 | prometheus.MustRegister(onosRxBytesTotal) |
| 153 | prometheus.MustRegister(onosTxPacketsTotal) |
| 154 | prometheus.MustRegister(onosRxPacketsTotal) |
| 155 | prometheus.MustRegister(onosTxDropPacketsTotal) |
| 156 | prometheus.MustRegister(onosRxDropPacketsTotal) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 157 | |
| 158 | prometheus.MustRegister(onosaaaRxAcceptResponses) |
| 159 | prometheus.MustRegister(onosaaaRxRejectResponses) |
| 160 | prometheus.MustRegister(onosaaaRxChallengeResponses) |
| 161 | prometheus.MustRegister(onosaaaTxAccessRequests) |
| 162 | prometheus.MustRegister(onosaaaRxInvalidValidators) |
| 163 | prometheus.MustRegister(onosaaaRxUnknownType) |
| 164 | prometheus.MustRegister(onosaaaPendingRequests) |
| 165 | prometheus.MustRegister(onosaaaRxDroppedResponses) |
| 166 | prometheus.MustRegister(onosaaaRxMalformedResponses) |
| 167 | prometheus.MustRegister(onosaaaRxUnknownserver) |
| 168 | prometheus.MustRegister(onosaaaRequestRttMillis) |
| 169 | prometheus.MustRegister(onosaaaRequestReTx) |
Daniele Moro | be24258 | 2019-10-01 14:02:46 -0700 | [diff] [blame] | 170 | |
Daniele Moro | 707c209 | 2019-12-06 16:39:18 -0800 | [diff] [blame] | 171 | prometheus.MustRegister(onosBngUpTxBytes) |
| 172 | prometheus.MustRegister(onosBngUpTxPackets) |
| 173 | prometheus.MustRegister(onosBngUpDropBytes) |
| 174 | prometheus.MustRegister(onosBngUpDropPackets) |
| 175 | prometheus.MustRegister(onosBngControlPackets) |
| 176 | prometheus.MustRegister(onosBngDownRxBytes) |
| 177 | prometheus.MustRegister(onosBngDownRxPackets) |
| 178 | prometheus.MustRegister(onosBngDownTxBytes) |
| 179 | prometheus.MustRegister(onosBngDownTxPackets) |
| 180 | prometheus.MustRegister(onosBngDownDropPackets) |
| 181 | prometheus.MustRegister(onosBngDownDropBytes) |
Ganesh Bhure | f15383e | 2019-11-11 15:15:14 +0530 | [diff] [blame] | 182 | |
| 183 | prometheus.MustRegister(deviceLaserBiasCurrent) |
| 184 | prometheus.MustRegister(deviceTemperature) |
| 185 | prometheus.MustRegister(deviceTxPower) |
| 186 | prometheus.MustRegister(deviceVoltage) |
Shubham Sharma | 57600bf | 2019-08-09 07:09:57 +0000 | [diff] [blame] | 187 | |
| 188 | prometheus.MustRegister(onosaaaRxEapolLogoff) |
| 189 | prometheus.MustRegister(onosaaaTxEapolResIdentityMsg) |
| 190 | prometheus.MustRegister(onosaaaTxAuthSuccess) |
| 191 | prometheus.MustRegister(onosaaaTxAuthFailure) |
| 192 | prometheus.MustRegister(onosaaaTxStartReq) |
| 193 | prometheus.MustRegister(onosaaaEapPktTxAuthChooseEap) |
| 194 | prometheus.MustRegister(onosaaaTxRespnotNak) |
shubham sharma | e7f8575 | 2019-09-16 13:37:18 +0000 | [diff] [blame] | 195 | |
kesavand | 6753e5e | 2020-04-28 03:34:21 -0400 | [diff] [blame] | 196 | prometheus.MustRegister(onosaaaEapolFramesTx) |
shubham sharma | e7f8575 | 2019-09-16 13:37:18 +0000 | [diff] [blame] | 197 | prometheus.MustRegister(onosaaaAuthStateIdle) |
| 198 | prometheus.MustRegister(onosaaaRequestIdFramesTx) |
| 199 | prometheus.MustRegister(onosaaaRequestEapFramesTx) |
| 200 | prometheus.MustRegister(onosaaaInvalidPktType) |
| 201 | prometheus.MustRegister(onosaaaInvalidBodyLength) |
| 202 | prometheus.MustRegister(onosaaaValidEapolFramesRx) |
| 203 | prometheus.MustRegister(onosaaaPendingResSupplicant) |
| 204 | prometheus.MustRegister(onosaaaRxResIdEapFrames) |
kesavand | 2cde658 | 2020-06-22 04:56:23 -0400 | [diff] [blame] | 205 | |
| 206 | //device metrics |
| 207 | //TODO: Check if component level temperatures are supported by Devices,If not remove in later versions of exporter |
| 208 | prometheus.MustRegister(oltDeviceCpuTemp) |
| 209 | prometheus.MustRegister(oltDeviceCpuUsagePercent) |
| 210 | prometheus.MustRegister(oltDeviceFanSpeed) |
| 211 | prometheus.MustRegister(oltDeviceDiskTemp) |
| 212 | prometheus.MustRegister(oltDeviceDiskUsagePercent) |
| 213 | prometheus.MustRegister(oltDeviceRamTemp) |
| 214 | prometheus.MustRegister(oltDeviceRamUsagePercent) |
| 215 | prometheus.MustRegister(oltDevicePowerUsagePercent) |
| 216 | prometheus.MustRegister(oltDeviceInnerSurroundTemp) |
| 217 | prometheus.MustRegister(oltDevicePowerUsage) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 220 | func loadConfigFile() Config { |
| 221 | m := Config{} |
| 222 | // this file path is configmap mounted in pod yaml |
| 223 | yamlFile, err := ioutil.ReadFile("/etc/config/conf.yaml") |
| 224 | if err != nil { |
| 225 | log.Printf("yamlFile.Get err: %v ", err) |
| 226 | } |
| 227 | err = yaml.Unmarshal(yamlFile, &m) |
| 228 | if err != nil { |
| 229 | log.Fatalf("Unmarshal: %v", err) |
| 230 | } |
| 231 | return m |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 232 | } |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 233 | |
kesavand | 0c06492 | 2020-12-15 15:36:25 +0530 | [diff] [blame^] | 234 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 235 | func main() { |
| 236 | // load configuration |
| 237 | conf := loadConfigFile() |
| 238 | |
| 239 | // logger setup |
| 240 | logger.Setup(conf.Logger.Host, strings.ToUpper(conf.Logger.LogLevel)) |
| 241 | logger.Info("Connecting to broker: [%s]", conf.Broker.Host) |
kesavand | 0c06492 | 2020-12-15 15:36:25 +0530 | [diff] [blame^] | 242 | utils.OnuSNhex = conf.Conv.Onusnhex |
| 243 | logger.Info("The utils.OnuSNhex : [%t]", utils.OnuSNhex) |
| 244 | logger.Info("The conf.Conv.Onusnformat is : [%t]", conf.Conv.Onusnhex) |
| 245 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 246 | |
| 247 | go kafkaInit(conf.Broker) |
| 248 | runServer(conf.Target) |
Ganesh Bhure | 0f2449c | 2019-07-15 10:31:38 +0530 | [diff] [blame] | 249 | } |