Skip to content

Commit

Permalink
Regard all filesets with the same name as one fileset
Browse files Browse the repository at this point in the history
Previously, filesets would be indexed by their ID. We shouldn't do that, as there can be multiple filesets with the same name and a different id
in the database. This happens when fileset include/exclude configurations are changed.

Fixes #10
  • Loading branch information
vierbergenlars authored and roock committed Apr 13, 2023
1 parent 9d9c967 commit b4539b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
20 changes: 10 additions & 10 deletions dataaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ type sqlQueries struct {

var queries map[string]*sqlQueries = map[string]*sqlQueries{
"mysql": &sqlQueries{
JobList: "SELECT j.Name, j.Type, j.ClientId, j.FileSetId, COALESCE(c.Name, ''), COALESCE(f.FileSet, ''), COUNT(*), SUM(j.JobBytes), SUM(j.JobFiles) FROM Job j LEFT JOIN Client c ON c.ClientId = j.ClientId LEFT JOIN FileSet f ON f.FileSetId = j.FileSetId GROUP BY j.Name, j.Type, j.ClientId, j.FileSetId, c.Name, f.FileSet HAVING MAX(j.SchedTime) >= ?",
LastJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId = ? ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId = ? AND JobStatus IN('T', 'W') ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulFullJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId = ? AND JobStatus IN('T', 'W') AND Level = 'F' ORDER BY StartTime DESC LIMIT 1",
JobList: "SELECT j.Name, j.Type, j.ClientId, COALESCE(c.Name, ''), COALESCE(f.FileSet, ''), COUNT(*), SUM(j.JobBytes), SUM(j.JobFiles) FROM Job j LEFT JOIN Client c ON c.ClientId = j.ClientId LEFT JOIN FileSet f ON f.FileSetId = j.FileSetId GROUP BY j.Name, j.Type, j.ClientId, c.Name, f.FileSet HAVING MAX(j.SchedTime) >= ?",
LastJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId IN(SELECT f.FileSetId FROM FileSet f WHERE f.FileSet = ?) ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId IN(SELECT f.FileSetId FROM FileSet f WHERE f.FileSet = ?) AND JobStatus IN('T', 'W') ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulFullJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime,COALESCE(EndTime, NOW()) FROM Job WHERE Name = ? AND ClientId = ? AND FileSetId IN(SELECT f.FileSetId FROM FileSet f WHERE f.FileSet = ?) AND JobStatus IN('T', 'W') AND Level = 'F' ORDER BY StartTime DESC LIMIT 1",
PoolInfo: "SELECT p.name, sum(m.volbytes) AS bytes, count(*) AS volumes, (not exists(select * from JobMedia jm where jm.mediaid = m.mediaid)) AS prunable, COALESCE(TIMESTAMPADD(SECOND, m.volretention, m.lastwritten) < NOW(), false) AS expired FROM Media m LEFT JOIN Pool p ON m.poolid = p.poolid GROUP BY p.name, prunable, expired",
JobStates: "SELECT JobStatus FROM Status",
},
"postgres": &sqlQueries{
JobList: "SELECT j.Name, j.Type, j.ClientId, j.FileSetId, COALESCE(c.Name, ''), COALESCE(f.FileSet, ''), COUNT(*), SUM(j.JobBytes), SUM(j.JobFiles) FROM job j LEFT JOIN client c ON c.ClientId = j.ClientId LEFT JOIN fileset f ON f.FileSetId = j.FileSetId GROUP BY j.Name, j.Type, j.ClientId, j.FileSetId, c.Name, f.FileSet HAVING MAX(j.SchedTime) >= $1",
LastJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId = $3 ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId = $3 AND JobStatus IN('T', 'W') ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulFullJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId = $3 AND JobStatus IN('T', 'W') AND Level = 'F' ORDER BY StartTime DESC LIMIT 1",
JobList: "SELECT j.Name, j.Type, j.ClientId, COALESCE(c.Name, ''), COALESCE(f.FileSet, ''), COUNT(*), SUM(j.JobBytes), SUM(j.JobFiles) FROM job j LEFT JOIN client c ON c.ClientId = j.ClientId LEFT JOIN fileset f ON f.FileSetId = j.FileSetId GROUP BY j.Name, j.Type, j.ClientId, c.Name, f.FileSet HAVING MAX(j.SchedTime) >= $1",
LastJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId IN(SELECT f.FileSetId from FileSet f WHERE f.FileSet = $3) ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId IN(SELECT f.FileSetId from FileSet f WHERE f.FileSet = $3) AND JobStatus IN('T', 'W') ORDER BY StartTime DESC LIMIT 1",
LastSuccessfulFullJob: "SELECT JobStatus,JobBytes,JobFiles,JobErrors,StartTime::timestamptz,COALESCE(EndTime::timestamptz, NOW()) FROM job WHERE Name = $1 AND ClientId = $2 AND FileSetId IN(SELECT f.FileSetId from FileSet f WHERE f.FileSet = $3) AND JobStatus IN('T', 'W') AND Level = 'F' ORDER BY StartTime DESC LIMIT 1",
PoolInfo: "SELECT p.name, sum(m.volbytes) AS bytes, count(m) AS volumes, (not exists(select * from jobmedia jm where jm.mediaid = m.mediaid)) AS prunable, COALESCE((m.lastwritten + (m.volretention * interval '1s')) < NOW(), false) as expired FROM media m LEFT JOIN pool p ON m.poolid = p.poolid GROUP BY p.name, prunable, expired",
JobStates: "SELECT JobStatus FROM status",
},
Expand Down Expand Up @@ -83,7 +83,7 @@ func (connection Connection) JobList() ([]JobInfo, error) {

for results.Next() {
var jobInfo JobInfo
err = results.Scan(&jobInfo.JobName, &jobInfo.JobType, &jobInfo.clientId, &jobInfo.fileSetId, &jobInfo.ClientName, &jobInfo.FileSetName, &jobInfo.TotalCount, &jobInfo.TotalBytes, &jobInfo.TotalFiles)
err = results.Scan(&jobInfo.JobName, &jobInfo.JobType, &jobInfo.clientId, &jobInfo.ClientName, &jobInfo.FileSetName, &jobInfo.TotalCount, &jobInfo.TotalBytes, &jobInfo.TotalFiles)
if err != nil {
return nil, err
}
Expand All @@ -105,7 +105,7 @@ func (connection Connection) execQuery(query string, args ...interface{}) (*sql.
}

func (connection Connection) execJobLookupQuery(query string, lookup *JobInfo) (*sql.Rows, error) {
return connection.execQuery(query, lookup.JobName, lookup.clientId, lookup.fileSetId)
return connection.execQuery(query, lookup.JobName, lookup.clientId, lookup.FileSetName)
}

func (connection Connection) execLastJobLookupQuery(query string, lookup *JobInfo) (*LastJob, error) {
Expand Down
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ var (
jobDiscoveryDays = flag.Int("job-discovery-days", 7, "Number of days in the past that will be searched for jobs")
)

func init() {
flag.Usage = func() {
fmt.Println("Usage: bareos_exporter [ ... ]\n\nParameters:")
fmt.Println()
flag.PrintDefaults()
}
}

func splitDsn(dsn string) (string, string, error) {
var splitDsn = strings.SplitN(dsn, "://", 2)
if len(splitDsn) != 2 {
Expand All @@ -44,12 +36,12 @@ func main() {

dbType, connectionString, err := splitDsn(*databaseURL)
if err != nil {
panic(err.Error())
log.Fatal(err)
}

connection, err := GetConnection(dbType, connectionString, *jobDiscoveryDays)
if err != nil {
panic(err.Error())
log.Fatal(err)
}
defer connection.Close()
collector := bareosCollector(connection)
Expand Down
13 changes: 6 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ const (
)

type JobLookup struct {
JobName string
clientId int
fileSetId int
JobName string
clientId int
FileSetName string
}

// JobInfo models query results for static values of a job
type JobInfo struct {
JobLookup
JobType JobType
JobName string
ClientName string
FileSetName string
JobType JobType
JobName string
ClientName string

TotalCount int
TotalBytes int
Expand Down

0 comments on commit b4539b4

Please sign in to comment.