Skip to content

Commit

Permalink
Updated logging code
Browse files Browse the repository at this point in the history
Logging config can now be modified on the fly when reloading.
  • Loading branch information
davidcoles committed Jul 25, 2024
1 parent a7cca80 commit fffc564
Show file tree
Hide file tree
Showing 8 changed files with 741 additions and 411 deletions.
22 changes: 18 additions & 4 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ func vipStatus(in map[VIP][]Serv, foo map[netip.Addr]State) (out []VIPStats) {
return
}

func vipState(services []cue.Service, old map[netip.Addr]State, logs *logger) map[netip.Addr]State {
F := "vips"
//func vipState(services []cue.Service, old map[netip.Addr]State, priorities map[netip.Addr]priority, logs *logger) map[netip.Addr]State {
func vipState(services []cue.Service, old map[netip.Addr]State, priorities map[netip.Addr]priority, logs Logger) map[netip.Addr]State {
facility := "vips"

rib := map[netip.Addr]bool{}
new := map[netip.Addr]State{}
Expand All @@ -190,19 +191,32 @@ func vipState(services []cue.Service, old map[netip.Addr]State, logs *logger) ma
}

for _, v := range cue.AllVIPs(services) {
p, _ := priorities[v]
log := logs.ERR

switch p {
case CRITICAL:
log = logs.ERR
case HIGH:
log = logs.WARNING
case MEDIUM:
log = logs.NOTICE
case LOW:
log = logs.INFO
}

if o, ok := old[v]; ok {
up, _ := rib[v]

if o.up != up {
new[v] = State{up: up, time: time.Now()}
logs.WARNING(F, KV{"vip": v, "state": updown(up), "event": "vip"})
log(facility, KV{"vip": v, "state": updown(up), "event": "vip"})
} else {
new[v] = o
}

} else {
logs.NOTICE(F, KV{"vip": v, "state": updown(rib[v]), "event": "vip"})
log(facility, KV{"vip": v, "state": updown(rib[v]), "event": "vip"})
new[v] = State{up: rib[v], time: time.Now()}
}
}
Expand Down
74 changes: 70 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ type Service struct {
// A short description of the service
Description string `json:"description,omitempty"`

// The minimum number of real servers which need to be health to consider this service viable
// critical, high, medium or low (default is critical)
Priority priority `json:"priority"`

// The minimum number of real servers which need to be healthy to consider this service viable
Need uint8 `json:"need,omitempty"`

// The set of backend server addresses and corresponding healthchecks which comprise this service
//RIPs map[string]Checks `json:"rips,omitempty"`
// Backend servers and corresponding health checks
Destinations map[ipport]Real `json:"reals,omitempty"`

// If set to true, the backend selection algorithm will not include layer 4 port numbers
Expand All @@ -85,13 +87,17 @@ type Config struct {
Multicast string `json:"multicast,omitempty"`
Webserver string `json:"webserver,omitempty"`
Webroot string `json:"webroot,omitempty"`
Logging logger `json:"logging,omitempty"`
Logging logging `json:"logging,omitempty"`
Native bool `json:"native,omitempty"`
Untagged bool `json:"untagged,omitempty"`
Address string `json:"address,omitempty"`
Interfaces []string `json:"interfaces,omitempty"`
}

func (c *Config) logging() Logging {
return c.Logging.logging()
}

func (c *Config) bgp(asn uint16, mp bool) map[string]bgp.Parameters {
if asn > 0 {
return map[string]bgp.Parameters{"127.0.0.1": bgp.Parameters{ASNumber: asn, HoldTime: 4, Multiprotocol: mp}}
Expand All @@ -110,6 +116,26 @@ func (c *Config) vlans() map[uint16]net.IPNet {
return ret
}

func (c *Config) priorities() map[netip.Addr]priority {

priorities := map[netip.Addr]priority{}

for k, v := range c.Services {
p, ok := priorities[k.Address]

if !ok {
p = LOW
priorities[k.Address] = p
}

if v.Priority < p {
priorities[k.Address] = v.Priority
}
}

return priorities
}

// Reads the load-balancer configuration from a JSON file. Returns a
// pointer to the Config object on success, and sets the error to
// non-nil on failure.
Expand Down Expand Up @@ -465,3 +491,43 @@ func (p protocol) string() string {
}
return fmt.Sprintf("%d", p)
}

type priority uint8

const CRITICAL = 0
const HIGH = 1
const MEDIUM = 2
const LOW = 3

func (p *priority) UnmarshalText(data []byte) error {

text := string(data)

switch text {
case "critical":
*p = CRITICAL
case "high":
*p = HIGH
case "medium":
*p = MEDIUM
case "low":
*p = LOW
default:
return errors.New("Invalid prority")
}
return nil
}

func (p priority) MarshalText() ([]byte, error) {
switch p {
case CRITICAL:
return []byte("critcal"), nil
case HIGH:
return []byte("high"), nil
case MEDIUM:
return []byte("medium"), nil
case LOW:
return []byte("low"), nil
}
return nil, errors.New("Invalid prority")
}
7 changes: 7 additions & 0 deletions cmd/config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sub services {
$defaults{_expc} = key($s, 'expect', undef); # checks
$defaults{_name} = key($s, 'name', undef);
$defaults{_desc} = key($s, 'description', undef);
$defaults{_prio} = key($s, 'priority', undef);
$defaults{_need} = key($s, 'need', 1)+0;
$defaults{_stic} = key($s, 'sticky', JSON::false);
$defaults{_rest} = key($s, 'reset', JSON::false);
Expand Down Expand Up @@ -157,6 +158,7 @@ sub services {

$svc->{'name'} = $p->{_name} if defined $p->{_name};
$svc->{'description'} = $p->{_desc} if defined $p->{_desc};
$svc->{'priority'} = $p->{_prio} if defined $p->{_prio};
$svc->{'scheduler'} = $p->{_schd} if defined $p->{_schd};
$svc->{'persist'} = $p->{_pers}+0 if defined $p->{_pers};
$svc->{'sticky'} = jsonbool($p->{_stic}) if defined $p->{_stic};
Expand All @@ -174,6 +176,10 @@ sub services {
if(!defined $opt{'n'} && $bind != $p->{_port}) {
die "port mismatch! enable port mapping for non DSR with -n";
}

if(defined $svc->{'priority'} && $svc->{'priority'} !~ /^(critical|high|medium|low)$/) {
die "Invalid priority: ".$svc->{'priority'}."\n";
}

foreach my $s (sort keys %servers) {
$rips{$s.":$bind"} = {
Expand Down Expand Up @@ -358,6 +364,7 @@ ()
_need => key($policy, 'need', $defaults->{_need}),
_name => key($policy, 'name', $defaults->{_name}),
_desc => key($policy, 'description', $defaults->{_desc}),
_prio => key($policy, 'priority', $defaults->{_prio}),
_bind => key($policy, 'bind', $port)+0,
_chks => [ checks($tcp, $port, $type, $policy, \%defaults, @checks) ],
};
Expand Down
55 changes: 52 additions & 3 deletions cmd/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"context"
"errors"
//"errors"
"bytes"
"fmt"
//"log"
"strings"
//"strings"
"sync"
"sync/atomic"
"time"
//"time"

"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
Expand All @@ -30,10 +31,13 @@ type Elasticsearch struct {
fail atomic.Uint64
}

/*
func (e *Elasticsearch) Fail() uint64 {
return e.fail.Load()
}
*/

/*
func (e *Elasticsearch) log(l string, hostname string) (err error) {
e.mutex.Lock()
Expand Down Expand Up @@ -79,7 +83,9 @@ func (e *Elasticsearch) log(l string, hostname string) (err error) {
return nil
}
*/

/*
func elastic(client *elasticsearch.Client, index, hostname string, fail *atomic.Uint64) chan string {
id := uint64(time.Now().UnixNano())
Expand All @@ -100,7 +106,9 @@ func elastic(client *elasticsearch.Client, index, hostname string, fail *atomic.
return in
}
*/

/*
func indexRequest(ctx context.Context, client *elasticsearch.Client, id uint64, index, host, message string) bool {
//ctx := context.Background()
Expand All @@ -123,3 +131,44 @@ func indexRequest(ctx context.Context, client *elasticsearch.Client, id uint64,
return false
}
*/

func (e *Elasticsearch) start() (err error) {

e.client, err = elasticsearch.NewClient(elasticsearch.Config{
Addresses: e.Addresses,
Username: string(e.Username),
Password: string(e.Password),
})

if err != nil {
return err
}

return nil
}

func (e *Elasticsearch) log(host string, id uint64, body []byte) bool {
if e.client == nil {
return false
}

ctx := context.Background()
req := esapi.IndexRequest{
Index: e.Index,
DocumentID: fmt.Sprintf("%s-%d", host, id),
Body: bytes.NewReader(body),
Refresh: "true",
}

res, err := req.Do(ctx, e.client)

if err != nil {
return false
}

defer res.Body.Close()

return res.StatusCode == 201
}
Loading

0 comments on commit fffc564

Please sign in to comment.