Skip to content

Commit

Permalink
INFRAOP-0: Altered Last Alertlogaccess
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Neumann committed May 17, 2018
1 parent bfd2887 commit e3fe145
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
oracle.conf
access.conf
prometheus_oracle_exporter
*.dat
*.log
96 changes: 66 additions & 30 deletions alertlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,79 @@ import (
"time"
"regexp"
"strings"
"io/ioutil"
"strconv"
"github.com/prometheus/common/log"
)


type Client struct {
Ip string `yaml:"ip"`
Date string `yaml:"date"`
}

type Lastlog struct {
Instance string `yaml:"instance"`
Clients []Client `yaml:"clients"`
}

type Lastlogs struct {
Cfgs []Lastlog `yaml:"lastlog"`
}

type oraerr struct {
ora string
text string
ignore string
count int
}

var Errors []oraerr
var (
Errors []oraerr
oralayout = "Mon Jan 02 15:04:05 2006"
lastlog Lastlogs
)


// Get individual ScrapeTime per Prometheus instance for alertlog
func (e *Exporter) GetLastScrapeTime(conf int) time.Time {
file := pwd + "/prometheus_" + config.Cfgs[conf].Instance + "_" + cleanIp(e.lastIp) + ".dat"
content, err := ioutil.ReadFile(file)
if err == nil {
t, _ := time.Parse("2006-01-02 15:04:05 -0700 MST",string(content))
return t
for i, _ := range lastlog.Cfgs {
if lastlog.Cfgs[i].Instance == config.Cfgs[conf].Instance {
for n, _ := range lastlog.Cfgs[i].Clients {
if lastlog.Cfgs[i].Clients[n].Ip == e.lastIp {
t, _ := time.Parse("2006-01-02 15:04:05 -0700 MST",string(lastlog.Cfgs[i].Clients[n].Date))
return t
}
}
}
}
return time.Now()
}

// Set individual ScrapeTime per Prometheus instance for alertlog
func (e *Exporter) SetLastScrapeTime(conf int,t time.Time) {
file := pwd + "/prometheus_" + config.Cfgs[conf].Instance + "_" + cleanIp(e.lastIp) + ".dat"
fh, err := os.Create(file)
if err == nil {
fh.WriteString(t.String())
fh.Close()
var indInst int = -1
var indIp int = -1
for i, _ := range lastlog.Cfgs {
if lastlog.Cfgs[i].Instance == config.Cfgs[conf].Instance {
indInst = i
for n, _ := range lastlog.Cfgs[i].Clients {
if lastlog.Cfgs[i].Clients[n].Ip == e.lastIp {
indIp = n
}
}
}
}
if indInst == -1 {
cln := Client{Ip: e.lastIp, Date: t.String()}
lastlog.Cfgs = append(lastlog.Cfgs, Lastlog{Instance: config.Cfgs[conf].Instance,
Clients: []Client{ cln } } )
}else{
if indIp == -1 {
cln := Client{Ip: e.lastIp, Date: t.String()}
lastlog.Cfgs[indInst].Clients = append(lastlog.Cfgs[indInst].Clients, cln)
}else{
lastlog.Cfgs[indInst].Clients[indIp].Date = t.String()
}
}
}

Expand All @@ -51,29 +93,26 @@ func addError(conf int, ora string, text string){
if ! found {
ignore := "0"
for _ , e := range config.Cfgs[conf].Alertlog[0].Ignoreora {
if e == ora {
ignore = "1"
}
if e == ora {; ignore = "1"; }
}
i := strings.Index(text, " ")
if i < 0{
i = 0
}
ora := oraerr{ora: ora, text: text[i+1:], ignore: ignore, count: 1}
is := strings.Index(text, " ")
ip := strings.Index(text, ". ")
if is < 0 {; is = 0; }
if ip < 0 {; ip = len(text); }
ora := oraerr{ora: ora, text: text[is+1:ip-is], ignore: ignore, count: 1}
Errors = append (Errors, ora)
}
}

func (e *Exporter) ScrapeAlertlog() {
var noError bool
loc := time.Now().Location()
re := regexp.MustCompile(`ORA-[0-9]+`)

ReadAccess()
for conf, _ := range config.Cfgs {
var lastTime time.Time
Errors = nil
noError = true
lastScrapeTime := e.GetLastScrapeTime(conf)
lastScrapeTime := e.GetLastScrapeTime(conf).Add(time.Second)

file, err := os.Open(config.Cfgs[conf].Alertlog[0].File)
if err != nil {
Expand All @@ -89,7 +128,6 @@ func (e *Exporter) ScrapeAlertlog() {
if re.MatchString(scanner.Text()) {
ora := re.FindString(scanner.Text())
addError(conf,ora, scanner.Text())
noError = false
}
}
}
Expand All @@ -102,13 +140,11 @@ func (e *Exporter) ScrapeAlertlog() {
Errors[i].ora,
Errors[i].text,
Errors[i].ignore).Set(float64(Errors[i].count))
WriteLog(config.Cfgs[conf].Instance + "(" + Errors[i].ignore + "): " + Errors[i].ora + " - " + Errors[i].text)
WriteLog(config.Cfgs[conf].Instance +
"(" + Errors[i].ignore + "/" + strconv.Itoa(Errors[i].count) + "): " +
Errors[i].ora + " - " + Errors[i].text)
}
}
if noError {
e.alertlog.WithLabelValues(config.Cfgs[conf].Database,
config.Cfgs[conf].Instance,
"ORA-0000","normal, successful completion","1").Set(0)
}
}
WriteAccess()
}
27 changes: 1 addition & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,6 @@ const (
exporter = "exporter"
)

type Alert struct {
File string `yaml:"file"`
Ignoreora []string `yaml:"ignoreora"`
}

type Query struct {
Sql string `yaml:"sql"`
Name string `yaml:"name"`
}

type Config struct {
Connection string `yaml:"connection"`
Database string `yaml:"database"`
Instance string `yaml:"instance"`
Alertlog []Alert `yaml:"alertlog"`
Queries []Query `yaml:"queries"`
db *sql.DB
}

type Configs struct {
Cfgs []Config `yaml:"connections"`
}

// Exporter collects Oracle DB metrics. It implements prometheus.Collector.
type Exporter struct {
duration, error prometheus.Gauge
Expand Down Expand Up @@ -71,10 +48,8 @@ var (
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("configfile", "oracle.conf", "ConfigurationFile in YAML format.")
logFile = flag.String("logfile", "exporter.log", "Logfile for parsed Oracle Alerts.")
accessFile = flag.String("accessfile", "access.conf", "Last access for parsed Oracle Alerts.")
landingPage = []byte("<html><head><title>Prometheus Oracle exporter</title></head><body><h1>Prometheus Oracle exporter</h1><p><a href='" + *metricPath + "'>Metrics</a></p></body></html>")
config Configs
oralayout = "Mon Jan 02 15:04:05 2006"
pwd string
)


Expand Down
60 changes: 53 additions & 7 deletions misc.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
package main

import (
"os"
"time"
"strings"
"io/ioutil"
"gopkg.in/yaml.v2"
"github.com/prometheus/common/log"
"os"
"time"
"path/filepath"
"database/sql"
"github.com/prometheus/common/log"
_ "github.com/mattn/go-oci8"
)

type Alert struct {
File string `yaml:"file"`
Ignoreora []string `yaml:"ignoreora"`
}

type Query struct {
Sql string `yaml:"sql"`
Name string `yaml:"name"`
}

type Config struct {
Connection string `yaml:"connection"`
Database string `yaml:"database"`
Instance string `yaml:"instance"`
Alertlog []Alert `yaml:"alertlog"`
Queries []Query `yaml:"queries"`
db *sql.DB
}

type Configs struct {
Cfgs []Config `yaml:"connections"`
}

var (
config Configs
pwd string
)

// Oracle gives us some ugly names back. This function cleans things up for Prometheus.
Expand Down Expand Up @@ -46,11 +76,27 @@ func loadConfig() bool {
}
}

func ReadAccess(){
var file = pwd + "/" + *accessFile
content, err := ioutil.ReadFile(file)
if err == nil {
err := yaml.Unmarshal(content, &lastlog)
if err != nil {
log.Fatalf("error1: %v", err)
}
}
}

func WriteAccess(){
content, _ := yaml.Marshal(&lastlog)
ioutil.WriteFile(pwd + "/" + *accessFile, content, 0644)
}

func WriteLog(message string) {
file := pwd + "/" + *logFile
fh, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
fh, err := os.OpenFile(pwd + "/" + *logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
fh.WriteString(time.Now().Format("2006-01-02 15:04:05") + " " + message + "\n")
fh.Close()
fh.Seek(0,2)
fh.WriteString(time.Now().Format("2006-01-02 15:04:05") + " " + message + "\n")
fh.Close()
}
}

0 comments on commit e3fe145

Please sign in to comment.