-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8d065
commit 8ecfe02
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package agent | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"go.etcd.io/bbolt" | ||
) | ||
|
||
type bucketStatsHolder struct { | ||
Stats bbolt.BucketStats | ||
FillPercent float64 | ||
NumberOfKeys uint | ||
Size uint | ||
} | ||
|
||
type dbStatsHolder struct { | ||
Stats bbolt.TxStats | ||
Size int64 | ||
} | ||
|
||
type Stats struct { | ||
DB dbStatsHolder | ||
Buckets map[string]bucketStatsHolder | ||
} | ||
|
||
func GetStats(db *bbolt.DB) (*Stats, error) { | ||
stats := &Stats{ | ||
Buckets: make(map[string]bucketStatsHolder), | ||
} | ||
|
||
if err := db.View(func(tx *bbolt.Tx) error { | ||
stats.DB.Stats = tx.Stats() | ||
stats.DB.Size = tx.Size() | ||
|
||
if err := tx.ForEach(bucketStatsFunc(stats)); err != nil { | ||
return errors.Wrap(err, "dumping bucket") | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, errors.Wrap(err, "creating view tx") | ||
} | ||
|
||
return stats, nil | ||
} | ||
|
||
func bucketStatsFunc(stats *Stats) func([]byte, *bbolt.Bucket) error { | ||
return func(name []byte, b *bbolt.Bucket) error { | ||
numKeys := uint(0) | ||
sizeValues := uint(0) | ||
if err := b.ForEach(func(_, v []byte) error { | ||
numKeys = numKeys + 1 | ||
sizeValues = sizeValues + uint(len(v)) | ||
return nil | ||
}); err != nil { | ||
return errors.Wrap(err, "foreach kv") | ||
} | ||
|
||
stats.Buckets[string(name)] = bucketStatsHolder{ | ||
Stats: b.Stats(), | ||
FillPercent: b.FillPercent, | ||
NumberOfKeys: numKeys, | ||
Size: sizeValues, | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package table | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/kolide/launcher/pkg/agent" | ||
"github.com/kolide/launcher/pkg/dataflatten" | ||
"github.com/kolide/launcher/pkg/osquery/tables/dataflattentable" | ||
"github.com/kolide/launcher/pkg/osquery/tables/tablehelpers" | ||
"github.com/kolide/osquery-go/plugin/table" | ||
"github.com/pkg/errors" | ||
"go.etcd.io/bbolt" | ||
) | ||
|
||
func LauncherDbInfo(db *bbolt.DB) *table.Plugin { | ||
columns := dataflattentable.Columns() | ||
return table.NewPlugin("kolide_launcher_db_info", columns, generateLauncherDbInfo(db)) | ||
} | ||
|
||
func generateLauncherDbInfo(db *bbolt.DB) table.GenerateFunc { | ||
return func(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { | ||
stats, err := agent.GetStats(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
statsJson, err := json.Marshal(stats) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var results []map[string]string | ||
|
||
for _, dataQuery := range tablehelpers.GetConstraints(queryContext, "query", tablehelpers.WithDefaults("*")) { | ||
flattenOpts := []dataflatten.FlattenOpts{ | ||
dataflatten.WithQuery(strings.Split(dataQuery, "/")), | ||
} | ||
|
||
rows, err := dataflatten.Json(statsJson, flattenOpts...) | ||
|
||
if err != nil { | ||
// In other places, we'd log the error | ||
// and continue. We don't have logs | ||
// here, so we'll just return it. | ||
return results, errors.Wrapf(err, "flattening with query %s", dataQuery) | ||
|
||
} | ||
|
||
results = append(results, dataflattentable.ToMap(rows, dataQuery, nil)...) | ||
} | ||
|
||
return results, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters