0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS ElasticBeanstalkリリース後旧環境の削除漏れ監視

Last updated at Posted at 2019-04-26

#マクロ
- {$EB_APP} : 環境名
- {$EB_ENVCNT} : 正常のアプリケーション数

#アイテム

  • タイプ:外部チェック
  • キー: http_get.sh -> パラメタをcurlに渡すだけ
    http_get.sh["https://api.hoge.com/v1/AwsEbEnvCount.aspx?acc=4&app={$EB_APP}&envcnt={$EB_ENVCNT}"]
  • 単位:文字列

#トリガー

  • 名前:[PF]EBリリース後の旧環境削除漏れ発生 {ITEM.LASTVALUE}
  • 条件式:10連続アウト
{[KEY].count(#10,"","ne")}=10

#API
zabbixのexternalscriptsにpythonなどのスクリプトを置くとまとまるが、C#で組んだAPIがすでにあったのでAPIを叩くことに。


using System.Web.Script.Serialization;
using Amazon.ElasticBeanstalk;

/*
    appName 監視APP名
    AmazonConfig 認証カスタムクラス 別途組んでください
    return 「""」以外はエラーする
*/
private string GetCountEnv(AmazonConfig.AccountNo acc, string appName, int envCount)
{
    var ret = new Dictionary<string, string>();

    if (envCount == 0)
    {
        envCount = 1;
    }

    AmazonConfig conf = new AmazonConfig(acc);
    var cl = new AmazonElasticBeanstalkClient(conf.AccessKeyID, conf.SecretAccessKey, conf.RegionEndpoint);
    var res = cl.DescribeEnvironments();

    //envCountより環境数が多いと削除漏れ!
    var envs = res.Environments.Where(s => s.Health == "Green").GroupBy(s => s.ApplicationName).Where(s => s.Key == appName && s.Count() > envCount);

    foreach (var i in envs)
    {
        ret.Add(i.Key, i.Count().ToString());
    }

    if (ret.Count > 0)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(ret);
    }
    else
    {
        return "";
    }
}

#メモ
これで削除漏れによる無駄な運用コストが減ったと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?