From 0cc92d0333d0eb0c4a921bd0eff21f3536276eb4 Mon Sep 17 00:00:00 2001 From: SudhanshuBawane Date: Wed, 17 Jan 2024 17:41:41 +0530 Subject: [PATCH 1/4] initial Signed-off-by: SudhanshuBawane --- agent/check_handler.go | 1 + asset/boltdb_manager.go | 7 ++ asset/set.go | 5 + .../CHANGELOG.md | 28 +++++ .../LICENSE | 7 ++ .../README.md | 107 ++++++++++++++++++ .../Desktop/sensu/agent/cache/assets.db | Bin 0 -> 32768 bytes .../raiden/Desktop/sensu/agent/cache/queue.db | Bin 0 -> 65536 bytes 8 files changed, 155 insertions(+) create mode 100644 home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/CHANGELOG.md create mode 100644 home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/LICENSE create mode 100644 home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/README.md create mode 100644 home/raiden/Desktop/sensu/agent/cache/assets.db create mode 100644 home/raiden/Desktop/sensu/agent/cache/queue.db diff --git a/agent/check_handler.go b/agent/check_handler.go index e9677beb2f..41c060bdb4 100644 --- a/agent/check_handler.go +++ b/agent/check_handler.go @@ -229,6 +229,7 @@ func (a *Agent) executeCheck(ctx context.Context, request *corev2.CheckRequest, logger.WithFields(fields).Debug("fetching assets for check") var err error assets, err = asset.GetAll(ctx, a.assetGetter, checkAssets) + logger.Println("======================info=====================\n", assets) if err != nil { a.sendFailure(event, fmt.Errorf("error getting assets for check: %s", err)) return diff --git a/asset/boltdb_manager.go b/asset/boltdb_manager.go index eb9e008db6..7188f40c22 100644 --- a/asset/boltdb_manager.go +++ b/asset/boltdb_manager.go @@ -121,7 +121,9 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // has proceeded to Update below will block here. if err := b.db.View(func(tx *bolt.Tx) error { // If the key exists, the bucket should already exist. + logger.Println("======================info=====================\n", assetBucketName) bucket := tx.Bucket(assetBucketName) + logger.Println("======================info=====================\n", bucket) if bucket == nil { return nil } @@ -130,6 +132,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run if value != nil { // deserialize asset if err := json.Unmarshal(value, &localAsset); err == nil { + logger.Println(err) return nil } } @@ -143,11 +146,13 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run if localAsset != nil { localAsset.Name = asset.Name localAsset.SHA512 = asset.Sha512 + logger.Println("======localAsset value========\n", localAsset) return localAsset, nil } if err := b.db.Update(func(tx *bolt.Tx) error { bucket, err := tx.CreateBucketIfNotExists(assetBucketName) + logger.Println(bucket, "========info========\n") if err != nil { return err } @@ -166,6 +171,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // install the asset tmpFile, err := b.fetchWithDuration(ctx, asset) + logger.Println(tmpFile) if err != nil { return err } @@ -188,6 +194,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // expand assetPath, err := b.expandWithDuration(tmpFile, asset) + logger.Println(assetPath, err, tmpFile, "-------info---------") if err != nil { return err } diff --git a/asset/set.go b/asset/set.go index 024a156355..3d10cac24e 100644 --- a/asset/set.go +++ b/asset/set.go @@ -31,6 +31,7 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) { scripts := make(map[string]io.ReadCloser) for _, asset := range r { err := filepath.Walk(asset.LibDir(), func(path string, info os.FileInfo, err error) error { + logger.Println("======================info=====================\n", err, path, asset.LibDir()) if strings.HasSuffix(path, ".js") { f, err := os.Open(path) if err != nil { @@ -50,12 +51,16 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) { // GetAll gets a list of assets with the provided getter. func GetAll(ctx context.Context, getter Getter, assets []types.Asset) (RuntimeAssetSet, error) { runtimeAssets := make([]*RuntimeAsset, 0, len(assets)) + logger.Println("======================info=====================\n", runtimeAssets) for _, asset := range assets { runtimeAsset, err := getter.Get(ctx, &asset) + logger.Println("======================info=====================\n", runtimeAsset) if err != nil { + logger.Println("======================info=====================\n", err) return nil, err } if runtimeAsset != nil { + logger.Println("======================info=====================\n", runtimeAsset) runtimeAssets = append(runtimeAssets, runtimeAsset) } } diff --git a/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/CHANGELOG.md b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/CHANGELOG.md new file mode 100644 index 0000000000..2342d2affe --- /dev/null +++ b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/CHANGELOG.md @@ -0,0 +1,28 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic +Versioning](http://semver.org/spec/v2.0.0.html). + +## Unreleased + +## [0.2.2] - 2021-03-31 + +### Changed +- Updated README + +## [0.2.1] - 2021-03-10 + +### Changed +- Change release github action to use macos-latest so that macos cgo build works + +## [0.2.0] - 2021-03-10 + +### Changed +- Updated goreleaser to build macOS version with cgo enabled + +## [0.1.0] - 2020-12-30 + +### Added +- Initial release diff --git a/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/LICENSE b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/LICENSE new file mode 100644 index 0000000000..03c76d189c --- /dev/null +++ b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Todd Campbell + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/README.md b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/README.md new file mode 100644 index 0000000000..c38fbcfb25 --- /dev/null +++ b/home/raiden/Desktop/sensu/agent/cache/24539739b5eb19bbab6eda151d0bcc63a0825afdfef3bc1ec3670c7b0a00fbbb2fd006d605a7a038b32269a22026d8947324f2bc0acdf35e8563cf4cb8660d7f/README.md @@ -0,0 +1,107 @@ +[![Sensu Bonsai Asset](https://img.shields.io/badge/Bonsai-Download%20Me-brightgreen.svg?colorB=89C967&logo=sensu)](https://bonsai.sensu.io/assets/sensu/check-cpu-usage) +![Go Test](https://github.com/sensu/check-cpu-usage/workflows/Go%20Test/badge.svg) +![goreleaser](https://github.com/sensu/check-cpu-usage/workflows/goreleaser/badge.svg) + +# Sensu CPU usage check + +## Table of Contents +- [Overview](#overview) +- [Usage examples](#usage-examples) +- [Configuration](#configuration) + - [Asset registration](#asset-registration) + - [Check definition](#check-definition) +- [Installation from source](#installation-from-source) +- [Contributing](#contributing) + +## Overview + +The Sensu CPU usage check is a [Sensu Check][1] that provides alerting and +metrics for CPU usage. Metrics are provided in [nagios_perfdata][5] format. + +**Note:** The macOS binary is built using [cgo][6] and may not be portable +across all versions of macOS. + +## Usage examples + +``` +Check CPU usage and provide metrics + +Usage: + check-cpu-usage [flags] + check-cpu-usage [command] + +Available Commands: + help Help about any command + version Print the version number of this plugin + +Flags: + -c, --critical float Critical threshold for overall CPU usage (default 90) + -w, --warning float Warning threshold for overall CPU usage (default 75) + -s, --sample-interval int Length of sample interval in seconds (default 2) + -h, --help help for check-cpu-usage + +Use "check-cpu-usage [command] --help" for more information about a command. +``` + +## Configuration + +### Asset registration + +[Sensu Assets][2] are the best way to make use of this plugin. If you're not +using an asset, please consider doing so! If you're using sensuctl 5.13 with +Sensu Backend 5.13 or later, you can use the following command to add the asset: + +``` +sensuctl asset add sensu/check-cpu-usage +``` + +If you're using an earlier version of sensuctl, you can find the asset on the +[Bonsai Asset Index][3]. + +### Check definition + +```yml +--- +type: CheckConfig +api_version: core/v2 +metadata: + name: check-cpu-usage + namespace: default +spec: + command: >- + check-cpu-usage + --critical 95 + --warning 85 + --sample-interval 2 + output_metric_format: nagios_perfdata + output_metric_handlers: + - influxdb + subscriptions: + - system + runtime_assets: + - sensu/check-cpu-usage +``` + +## Installation from source + +The preferred way of installing and deploying this plugin is to use it as an +Asset. If you would like to compile and install the plugin from source or +contribute to it, download the latest version or create an executable from this +source. + +From the local path of the check-cpu-usage repository: + +``` +go build +``` + +## Contributing + +For more information about contributing to this plugin, see [Contributing][4]. + +[1]: https://docs.sensu.io/sensu-go/latest/reference/checks/ +[2]: https://docs.sensu.io/sensu-go/latest/reference/assets/ +[3]: https://bonsai.sensu.io/assets/sensu/check-cpu-usage +[4]: https://github.com/sensu/sensu-go/blob/master/CONTRIBUTING.md +[5]: https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-schedule/collect-metrics-with-checks/#supported-output-metric-formats +[6]: https://golang.org/cmd/cgo/ diff --git a/home/raiden/Desktop/sensu/agent/cache/assets.db b/home/raiden/Desktop/sensu/agent/cache/assets.db new file mode 100644 index 0000000000000000000000000000000000000000..10e38e0ccb2171f6319e4dc085d75a55ff3bc8cd GIT binary patch literal 32768 zcmeI)J!{-B7{GBiZ88;_x!F8)3Rsp-vYoAvAwvi${RDdLm2#I-Unor|#t`qW(PPnCnI{M&b^FV)fU z&EofC?nj9L0tg_000IagfB*srAb<$r?LOCy82r6aoJy2_ETK0DsTNfR?i*y5bwvc_qvv2>^}!d zHUtnr009ILKmY**5I_I{1hyfN|L1pDtTO;|9zf0u*v37W0|5jOKmY**5I_I{1Q0*~ x0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R;X};14^lZdL#Q literal 0 HcmV?d00001 diff --git a/home/raiden/Desktop/sensu/agent/cache/queue.db b/home/raiden/Desktop/sensu/agent/cache/queue.db new file mode 100644 index 0000000000000000000000000000000000000000..b576e37a5cbaa0af722ba8215ca22f5e2befa50a GIT binary patch literal 65536 zcmeI)Jx;?g7y!^veuX;p2)#hXNUsx=R6=4vl^A*jCT_z9;s(szp%VuHyYaVFt&nJy zc6gC2JC5U6Z~JV=Rn=W*C0~9GmxEj$brt8OKC+8axGedqZ7NTGo^PhZk7=}%6Cgl< z009C72oNAZfB*pk$0Kl3hO#aGKV2JY#Qj&LKAtx(pWpBG+v{3$w*&|fAV7cs0RjXF z5FkK+z!3<<`u(8lN_jqDoKlYSbDr;SQ_A}Q#wj)JL%nF5RN8n&ck{Mx78_fn%4oFh zxq1Qw2oNAZfB*pk1PBlyKw!TDv3@^TMfzR5pSS*Br Date: Mon, 22 Jan 2024 18:07:36 +0530 Subject: [PATCH 2/4] error resolved but need polishing Signed-off-by: SudhanshuBawane --- agent/check_handler.go | 4 ++++ asset/boltdb_manager.go | 25 +++++++++++++++++++++---- asset/set.go | 8 ++++---- cli/commands/asset/add.go | 3 +++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/agent/check_handler.go b/agent/check_handler.go index 41c060bdb4..61b0b544eb 100644 --- a/agent/check_handler.go +++ b/agent/check_handler.go @@ -226,9 +226,13 @@ func (a *Agent) executeCheck(ctx context.Context, request *corev2.CheckRequest, if len(checkAssets) == 0 { logger.WithFields(fields).Debug("no assets defined for this check") } else { + logger.WithFields(fields).Debug("fetching assets for check") var err error assets, err = asset.GetAll(ctx, a.assetGetter, checkAssets) + + //sudhanshu/5009 + //check if asset.db exits if not then re-create it also delete the SHA associated with it and re-create it. logger.Println("======================info=====================\n", assets) if err != nil { a.sendFailure(event, fmt.Errorf("error getting assets for check: %s", err)) diff --git a/asset/boltdb_manager.go b/asset/boltdb_manager.go index 7188f40c22..d1f72aeeab 100644 --- a/asset/boltdb_manager.go +++ b/asset/boltdb_manager.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + sensupath "github.com/sensu/sensu-go/util/path" "os" "path/filepath" @@ -139,6 +140,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run return nil }); err != nil { + logger.Println("=====SUDHANSHU Get call return 1 =======", err) return nil, err } @@ -152,8 +154,10 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run if err := b.db.Update(func(tx *bolt.Tx) error { bucket, err := tx.CreateBucketIfNotExists(assetBucketName) - logger.Println(bucket, "========info========\n") + if err != nil { + logger.Println("=====SUDHANSHU CreateBucketIfNotExists call return 2 =======", err) + logger.Println(bucket, "========info========/n") return err } @@ -171,8 +175,10 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // install the asset tmpFile, err := b.fetchWithDuration(ctx, asset) - logger.Println(tmpFile) + if err != nil { + logger.Println("=====SUDHANSHU fetch With Duration call return 3 =======", err) + logger.Println(bucket, "========info========/n", tmpFile) return err } defer tmpFile.Close() @@ -180,9 +186,13 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // verify if err := b.verifier.Verify(tmpFile, asset.Sha512); err != nil { + logger.Println("=====SUDHANSHU File size verifier call return 4 =======", err) + logger.Println(bucket, "========info========") // Attempt to retrieve the size of the downloaded asset var size uint64 if fileInfo, err := tmpFile.Stat(); err == nil { + logger.Println("=====SUDHANSHU tmpFile stat call return 5 =======", err) + logger.Println(bucket, "========info========") size = uint64(fileInfo.Size()) } @@ -194,8 +204,10 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // expand assetPath, err := b.expandWithDuration(tmpFile, asset) - logger.Println(assetPath, err, tmpFile, "-------info---------") + if err != nil { + logger.Println("=====SUDHANSHU expand with path call return 5 =======", err, assetPath, tmpFile) + logger.Println(bucket, "========info========") return err } @@ -247,7 +259,12 @@ func (b *boltDBAssetManager) expandWithDuration(tmpFile *os.File, asset *corev2. Observe(v * float64(1000)) })) defer timer.ObserveDuration() - + aseetSHA := asset.Sha512 + fullPath := "/home/raiden/Desktop/sensu/agent/cache/" + aseetSHA + errorSHA := os.RemoveAll(fullPath) + logger.Println("=====cache dir =========", sensupath.UserCacheDir("sensuctl")) + logger.Println("========== SHA Error is =====================", errorSHA) + logger.Println() assetPath = filepath.Join(b.localStorage, asset.Sha512) return assetPath, b.expander.Expand(tmpFile, assetPath) } diff --git a/asset/set.go b/asset/set.go index 3d10cac24e..e1bb0cc1f9 100644 --- a/asset/set.go +++ b/asset/set.go @@ -51,16 +51,16 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) { // GetAll gets a list of assets with the provided getter. func GetAll(ctx context.Context, getter Getter, assets []types.Asset) (RuntimeAssetSet, error) { runtimeAssets := make([]*RuntimeAsset, 0, len(assets)) - logger.Println("======================info=====================\n", runtimeAssets) + logger.Println("======================info===================== 1", runtimeAssets) for _, asset := range assets { runtimeAsset, err := getter.Get(ctx, &asset) - logger.Println("======================info=====================\n", runtimeAsset) + logger.Println("======================info===================== 2: error", runtimeAsset, err) if err != nil { - logger.Println("======================info=====================\n", err) + logger.Println("======================info===================== 3 : error", err) return nil, err } if runtimeAsset != nil { - logger.Println("======================info=====================\n", runtimeAsset) + logger.Println("======================info===================== 4 : error", runtimeAsset, err) runtimeAssets = append(runtimeAssets, runtimeAsset) } } diff --git a/cli/commands/asset/add.go b/cli/commands/asset/add.go index 1ae0af1ffc..240aacd530 100644 --- a/cli/commands/asset/add.go +++ b/cli/commands/asset/add.go @@ -36,6 +36,9 @@ func AddCommand(cli *cli.SensuCli) *cobra.Command { func addCommandExecute(cli *cli.SensuCli) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { + //sudhanshu/5009 + //check if asset.db exits if not then re-create it also delete the SHA associated with it and re-create it. + // // If no name is present print out usage if len(args) != 1 { _ = cmd.Help() From 90be1090f7ab83c409257b0c0c789885f94cbf7d Mon Sep 17 00:00:00 2001 From: SudhanshuBawane Date: Tue, 23 Jan 2024 20:40:06 +0530 Subject: [PATCH 3/4] inter Signed-off-by: SudhanshuBawane --- agent/cmd/start.go | 2 +- asset/boltdb_manager.go | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/agent/cmd/start.go b/agent/cmd/start.go index 9ce7501a3d..58c7ba13f9 100644 --- a/agent/cmd/start.go +++ b/agent/cmd/start.go @@ -9,8 +9,8 @@ import ( "strings" "time" - "github.com/sensu/sensu-go/agent" corev2 "github.com/sensu/core/v2" + "github.com/sensu/sensu-go/agent" "github.com/sensu/sensu-go/asset" "github.com/sensu/sensu-go/util/path" "github.com/sensu/sensu-go/util/url" diff --git a/asset/boltdb_manager.go b/asset/boltdb_manager.go index d1f72aeeab..8f7e0eae1a 100644 --- a/asset/boltdb_manager.go +++ b/asset/boltdb_manager.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - sensupath "github.com/sensu/sensu-go/util/path" + "github.com/spf13/viper" "os" "path/filepath" @@ -24,6 +24,7 @@ const ( // ExpandDuration is the name of the prometheus summary vec used to track // average latencies of asset expansion. ExpandDuration = "sensu_go_asset_expand_duration" + FlagCacheDir = "cache-dir" ) var ( @@ -186,6 +187,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // verify if err := b.verifier.Verify(tmpFile, asset.Sha512); err != nil { + logger.Println("=====SUDHANSHU File size verifier call return 4 =======", err) logger.Println(bucket, "========info========") // Attempt to retrieve the size of the downloaded asset @@ -259,10 +261,18 @@ func (b *boltDBAssetManager) expandWithDuration(tmpFile *os.File, asset *corev2. Observe(v * float64(1000)) })) defer timer.ObserveDuration() - aseetSHA := asset.Sha512 - fullPath := "/home/raiden/Desktop/sensu/agent/cache/" + aseetSHA + assetSHA := asset.Sha512 + //fullPath := "/home/raiden/Desktop/sensu/agent/cache/" + assetSHA + + //cfg := agent.NewConfig() + //viper.SetDefault(FlagCacheDir, path.SystemCacheDir("sensu-agent")) + CacheDir := viper.GetString(FlagCacheDir) + fullPath := filepath.Join(CacheDir, assetSHA) errorSHA := os.RemoveAll(fullPath) - logger.Println("=====cache dir =========", sensupath.UserCacheDir("sensuctl")) + logger.Println() + + //logger.Println("=====cache dir =========", sensupath.UserCacheDir("sensuctl")) + logger.Println("======= cache dir=========", CacheDir, fullPath) logger.Println("========== SHA Error is =====================", errorSHA) logger.Println() assetPath = filepath.Join(b.localStorage, asset.Sha512) From 6fe811a3acd0743d159ea95bc5c2a22fe7fa4d39 Mon Sep 17 00:00:00 2001 From: SudhanshuBawane Date: Wed, 24 Jan 2024 17:33:38 +0530 Subject: [PATCH 4/4] final Signed-off-by: SudhanshuBawane --- agent/check_handler.go | 3 --- asset/boltdb_manager.go | 32 ++++++++------------------------ asset/expander.go | 15 +++++++++++++-- asset/expander_test.go | 34 ++++++++++++++++++++++++++++++++++ asset/set.go | 8 +++----- 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/agent/check_handler.go b/agent/check_handler.go index 61b0b544eb..ccb1f268fd 100644 --- a/agent/check_handler.go +++ b/agent/check_handler.go @@ -231,9 +231,6 @@ func (a *Agent) executeCheck(ctx context.Context, request *corev2.CheckRequest, var err error assets, err = asset.GetAll(ctx, a.assetGetter, checkAssets) - //sudhanshu/5009 - //check if asset.db exits if not then re-create it also delete the SHA associated with it and re-create it. - logger.Println("======================info=====================\n", assets) if err != nil { a.sendFailure(event, fmt.Errorf("error getting assets for check: %s", err)) return diff --git a/asset/boltdb_manager.go b/asset/boltdb_manager.go index 8f7e0eae1a..a8403d4262 100644 --- a/asset/boltdb_manager.go +++ b/asset/boltdb_manager.go @@ -123,9 +123,9 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // has proceeded to Update below will block here. if err := b.db.View(func(tx *bolt.Tx) error { // If the key exists, the bucket should already exist. - logger.Println("======================info=====================\n", assetBucketName) + bucket := tx.Bucket(assetBucketName) - logger.Println("======================info=====================\n", bucket) + if bucket == nil { return nil } @@ -141,7 +141,6 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run return nil }); err != nil { - logger.Println("=====SUDHANSHU Get call return 1 =======", err) return nil, err } @@ -149,7 +148,6 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run if localAsset != nil { localAsset.Name = asset.Name localAsset.SHA512 = asset.Sha512 - logger.Println("======localAsset value========\n", localAsset) return localAsset, nil } @@ -157,8 +155,6 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run bucket, err := tx.CreateBucketIfNotExists(assetBucketName) if err != nil { - logger.Println("=====SUDHANSHU CreateBucketIfNotExists call return 2 =======", err) - logger.Println(bucket, "========info========/n") return err } @@ -178,8 +174,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run tmpFile, err := b.fetchWithDuration(ctx, asset) if err != nil { - logger.Println("=====SUDHANSHU fetch With Duration call return 3 =======", err) - logger.Println(bucket, "========info========/n", tmpFile) + return err } defer tmpFile.Close() @@ -188,13 +183,9 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run // verify if err := b.verifier.Verify(tmpFile, asset.Sha512); err != nil { - logger.Println("=====SUDHANSHU File size verifier call return 4 =======", err) - logger.Println(bucket, "========info========") // Attempt to retrieve the size of the downloaded asset var size uint64 if fileInfo, err := tmpFile.Stat(); err == nil { - logger.Println("=====SUDHANSHU tmpFile stat call return 5 =======", err) - logger.Println(bucket, "========info========") size = uint64(fileInfo.Size()) } @@ -208,8 +199,6 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run assetPath, err := b.expandWithDuration(tmpFile, asset) if err != nil { - logger.Println("=====SUDHANSHU expand with path call return 5 =======", err, assetPath, tmpFile) - logger.Println(bucket, "========info========") return err } @@ -261,20 +250,15 @@ func (b *boltDBAssetManager) expandWithDuration(tmpFile *os.File, asset *corev2. Observe(v * float64(1000)) })) defer timer.ObserveDuration() - assetSHA := asset.Sha512 - //fullPath := "/home/raiden/Desktop/sensu/agent/cache/" + assetSHA - //cfg := agent.NewConfig() - //viper.SetDefault(FlagCacheDir, path.SystemCacheDir("sensu-agent")) + assetSHA := asset.Sha512 CacheDir := viper.GetString(FlagCacheDir) fullPath := filepath.Join(CacheDir, assetSHA) - errorSHA := os.RemoveAll(fullPath) - logger.Println() - //logger.Println("=====cache dir =========", sensupath.UserCacheDir("sensuctl")) - logger.Println("======= cache dir=========", CacheDir, fullPath) - logger.Println("========== SHA Error is =====================", errorSHA) - logger.Println() + if err := CleanUp(fullPath); err != nil { //fix for git issue 5009 + fmt.Errorf("error cleaning up the SHA dir: %s", err) + } + assetPath = filepath.Join(b.localStorage, asset.Sha512) return assetPath, b.expander.Expand(tmpFile, assetPath) } diff --git a/asset/expander.go b/asset/expander.go index d13df1b1cd..54efc2778b 100644 --- a/asset/expander.go +++ b/asset/expander.go @@ -3,9 +3,9 @@ package asset import ( "errors" "fmt" - "io" - archiver "github.com/mholt/archiver/v3" + "io" + "os" filetype "gopkg.in/h2non/filetype.v1" filetype_types "gopkg.in/h2non/filetype.v1/types" @@ -37,6 +37,17 @@ type namer interface { Name() string } +// Sudhanshu - CleanUp the SHA for the git issue 5009 fix. Making sure that the asset.db after creation gets updated properly. + +func CleanUp(fullPath string) error { + errorSHA := os.RemoveAll(fullPath) + if errorSHA != nil { + return errorSHA + } + return nil + +} + // Expand an archive to a target directory. func (a *archiveExpander) Expand(archive io.ReadSeeker, targetDirectory string) error { // detect the type of archive the asset is diff --git a/asset/expander_test.go b/asset/expander_test.go index 6d028bdf90..640cf014df 100644 --- a/asset/expander_test.go +++ b/asset/expander_test.go @@ -1,6 +1,7 @@ package asset import ( + v2 "github.com/sensu/core/v2" "os" "path/filepath" "testing" @@ -8,6 +9,39 @@ import ( "github.com/sensu/sensu-go/testing/testutil" ) +var asset *v2.Asset + +// sudhanshu- Git issue 5009 + +func TestCleanUp(t *testing.T) { + t.Parallel() + + // Create a temporary directory for testing + tmpDir := t.TempDir() + + // Define the SHA and file name + SHAName := "shaAsset.tar" + SHAFilePath := filepath.Join(tmpDir, SHAName) + + // Create a dummy file inside the temporary directory + SHAFile, err := os.Create(SHAFilePath) + if err != nil { + t.Fatalf("Failed to create dummy file: %v", err) + } + SHAFile.Close() + + // Call CleanUp with the SHA of the dummy file and the temporary directory + err = CleanUp(SHAFilePath) + if err != nil { + t.Errorf("CleanUp returned an error: %v", err) + } + + _, err = os.Stat(SHAFilePath) + if !os.IsNotExist(err) { + t.Errorf("CleanUp did not remove the dummy file as expected") + } +} + func TestExpandValidTar(t *testing.T) { t.Parallel() assetPath := getFixturePath("rubby-on-rails.tar") diff --git a/asset/set.go b/asset/set.go index e1bb0cc1f9..2fef75f676 100644 --- a/asset/set.go +++ b/asset/set.go @@ -31,7 +31,6 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) { scripts := make(map[string]io.ReadCloser) for _, asset := range r { err := filepath.Walk(asset.LibDir(), func(path string, info os.FileInfo, err error) error { - logger.Println("======================info=====================\n", err, path, asset.LibDir()) if strings.HasSuffix(path, ".js") { f, err := os.Open(path) if err != nil { @@ -51,16 +50,15 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) { // GetAll gets a list of assets with the provided getter. func GetAll(ctx context.Context, getter Getter, assets []types.Asset) (RuntimeAssetSet, error) { runtimeAssets := make([]*RuntimeAsset, 0, len(assets)) - logger.Println("======================info===================== 1", runtimeAssets) + for _, asset := range assets { runtimeAsset, err := getter.Get(ctx, &asset) - logger.Println("======================info===================== 2: error", runtimeAsset, err) + if err != nil { - logger.Println("======================info===================== 3 : error", err) + return nil, err } if runtimeAsset != nil { - logger.Println("======================info===================== 4 : error", runtimeAsset, err) runtimeAssets = append(runtimeAssets, runtimeAsset) } }