From bea8e5aba8fc84dcb0c980c3948ed6d78719dded Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 30 Jan 2020 17:44:42 +0100 Subject: Big rafactoring : code split in several modules and some other best practices --- state/header.go | 57 +++++++++++++++++++++++++++++++ state/job.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ state/parser.go | 40 ++++++++++++++++++++++ state/state.go | 21 ++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 state/header.go create mode 100644 state/job.go create mode 100644 state/parser.go create mode 100644 state/state.go (limited to 'state') diff --git a/state/header.go b/state/header.go new file mode 100644 index 0000000..3e55899 --- /dev/null +++ b/state/header.go @@ -0,0 +1,57 @@ +package state + +import ( + "bytes" + "encoding/binary" + "fmt" + "log" + "os" +) + +// c.StateFile()HeaderLength : the length of the state file header struct +const headerLength = 14 + 2 + 4 + 4 + 8 + 8 + 19*8 + +// header : A structure to hold the header of the state file. It is statically aligned for amd64 architecture +// This comes from bareos repository file core/src/lib/bsys.cc:525 and core/src/lib/bsys.cc:652 +type header struct { + ID [14]byte + _ int16 + Version int32 + _ int32 + LastJobsAddr uint64 + EndOfRecentJobResultsList uint64 + Reserved [19]uint64 +} + +func (sfh header) String() string { + return fmt.Sprintf("ID: \"%s\", Version: %d, LastJobsAddr: %d, EndOfRecentJobResultsList: %d", sfh.ID[:len(sfh.ID)-2], sfh.Version, sfh.EndOfRecentJobResultsList, sfh.Reserved) +} + +func (s *State) parseHeader(file *os.File) (err error) { + // Parsing the state file header + n, data, err := s.readNextBytes(file, headerLength) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : %s", err) + } + if n != headerLength { + return fmt.Errorf("INFO Corrupted state file : invalid header length in %s", s.config.StateFile()) + } + buffer := bytes.NewBuffer(data) + err = binary.Read(buffer, binary.LittleEndian, &s.header) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : binary.Read failed on header in %s : %s", s.config.StateFile(), err) + } + if s.config.Verbose() { + log.Printf("Parsed header: %+s\n", s.header) + } + if id := string(s.header.ID[:len(s.header.ID)-1]); id != "Bareos State\n" && id != "Bacula State\n" { + return fmt.Errorf("INFO Corrupted state file : Not a bareos or bacula state file %s", s.config.StateFile()) + } + if s.header.Version != 4 { + return fmt.Errorf("INFO Invalid state file : This script only supports bareos state file version 4, got %d", s.header.Version) + } + if s.header.LastJobsAddr == 0 { + return fmt.Errorf("INFO No jobs exist in the state file") + } + return +} diff --git a/state/job.go b/state/job.go new file mode 100644 index 0000000..8d5b04f --- /dev/null +++ b/state/job.go @@ -0,0 +1,103 @@ +package state + +import ( + "bareos-zabbix-check/job" + "bytes" + "encoding/binary" + "fmt" + "log" + "os" + "regexp" + "time" +) + +// jobLength : the length of the job result struct +const jobLength = 16 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 8 + 8 + 8 + maxNameLength + +var jobNameRegex = regexp.MustCompilePOSIX(`^([-A-Za-z0-9_]+)\.[0-9]{4}-[0-9]{2}-[0-9]{2}.*`) + +// jobEntry : A structure to hold a job result from the state file +// This comes from bareos repository file core/src/lib/recent_job_results_list.h:29 and file core/src/lib/recent_job_results_list.cc:44 +type jobEntry struct { + Pad [16]byte + Errors int32 + JobType int32 + JobStatus int32 + JobLevel int32 + JobID uint32 + VolSessionID uint32 + VolSessionTime uint32 + JobFiles uint32 + JobBytes uint64 + StartTime uint64 + EndTime uint64 + Job [maxNameLength]byte +} + +func (je jobEntry) String() string { + var matches = jobNameRegex.FindSubmatchIndex(je.Job[:]) + var jobNameLen int + if len(matches) >= 4 { + jobNameLen = matches[3] + } + return fmt.Sprintf("Errors: %d, JobType: %c, JobStatus: %c, JobLevel: %c, JobID: %d, VolSessionID: %d, VolSessionTime: %d, JobFiles: %d, JobBytes: %d, StartTime: %s, EndTime: %s, Job: %s", + je.Errors, je.JobType, je.JobStatus, je.JobLevel, je.JobID, je.VolSessionID, je.VolSessionTime, je.JobFiles, je.JobBytes, time.Unix(int64(je.StartTime), 0), time.Unix(int64(je.EndTime), 0), je.Job[:jobNameLen]) +} + +func (s *State) parseJobs(file *os.File) (err error) { + // We seek to the jobs position in the state file + file.Seek(int64(s.header.LastJobsAddr), 0) + + // We read how many jobs there are in the state file + n, data, err := s.readNextBytes(file, 4) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : %s", err) + } + if n != 4 { + return fmt.Errorf("INFO Corrupted state file : invalid numberOfJobs read length in %s", s.config.StateFile()) + } + buffer := bytes.NewBuffer(data) + var numberOfJobs uint32 + err = binary.Read(buffer, binary.LittleEndian, &numberOfJobs) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : binary.Read failed on numberOfJobs in %s : %s", s.config.StateFile(), err) + } + if s.config.Verbose() { + log.Printf("%d jobs found in state file\n", numberOfJobs) + } + + // We parse the job entries + for ; numberOfJobs > 0; numberOfJobs-- { + var ( + jobResult jobEntry + jobName string + ) + n, data, err = s.readNextBytes(file, jobLength) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : %s", err) + } + if n != jobLength { + return fmt.Errorf("INFO Corrupted state file : invalid job entry in %s", s.config.StateFile()) + } + buffer = bytes.NewBuffer(data) + err = binary.Read(buffer, binary.LittleEndian, &jobResult) + if err != nil { + return fmt.Errorf("INFO Corrupted state file : binary.Read failed on job entry in %s : %s", s.config.StateFile(), err) + } + matches := jobNameRegex.FindSubmatchIndex(jobResult.Job[:]) + if len(matches) >= 4 { + jobName = string(jobResult.Job[:matches[3]]) + } else { + return fmt.Errorf("INFO Couldn't parse job name, this shouldn't happen : %s", jobResult.Job[:]) + } + if s.config.Verbose() { + log.Printf("Parsed job entry: %s\n", jobResult) + } + // If the job is of type backup (B == ascii 66) + if jobResult.JobType == 66 { + // If the job is of status success JobStatus is equals to 84 (T == ascii 84) + s.jobs = append(s.jobs, job.Job{Name: jobName, Timestamp: jobResult.StartTime, Success: jobResult.JobStatus == 84}) + } + } + return +} diff --git a/state/parser.go b/state/parser.go new file mode 100644 index 0000000..60f5394 --- /dev/null +++ b/state/parser.go @@ -0,0 +1,40 @@ +package state + +import ( + "bareos-zabbix-check/config" + "fmt" + "os" +) + +// Parse parses a bareos state file +func (s *State) Parse(c *config.Config) (err error) { + s.config = c + // Open the state file + file, err := os.Open(c.StateFile()) + if err != nil { + return fmt.Errorf("INFO Couldn't open state file : %s", err) + } + defer file.Close() + + err = s.parseHeader(file) + if err != nil { + return err + } + err = s.parseJobs(file) + if err != nil { + return err + } + + return +} + +// readNextBytes : Reads the next "number" bytes from a "file", returns the number of bytes actually read as well as the bytes read +func (s *State) readNextBytes(file *os.File, number int) (n int, bytes []byte, err error) { + bytes = make([]byte, number) + n, err = file.Read(bytes) + if err != nil { + return 0, nil, fmt.Errorf("file.Read failed in %s : %s", s.config.StateFile(), err) + } + + return +} diff --git a/state/state.go b/state/state.go new file mode 100644 index 0000000..d28a701 --- /dev/null +++ b/state/state.go @@ -0,0 +1,21 @@ +package state + +import ( + "bareos-zabbix-check/config" + "bareos-zabbix-check/job" +) + +// maxNameLength : the maximum length of a string, hard coded in bareos +const maxNameLength = 128 + +// State is an object for manipulating a bareos state file +type State struct { + config *config.Config + header header + jobs []job.Job +} + +// Jobs returns the jobs from the state file +func (s *State) Jobs() []job.Job { + return s.jobs +} -- cgit v1.2.3