Skip to content

Instantly share code, notes, and snippets.

@handlename
Last active March 10, 2022 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handlename/cb5140c7d94c68d454ae768491208ded to your computer and use it in GitHub Desktop.
Save handlename/cb5140c7d94c68d454ae768491208ded to your computer and use it in GitHub Desktop.
convert mackerel api result to terraform resource definition
#!/usr/bin/env perl
use strict;
use warnings;
use JSON::XS;
use Text::MicroTemplate;
use Data::Dumper;
my $env = $ARGV[0];
my $tmts = {};
$tmts->{connectivity} = build_template(<<'EOH');
? my ($env, $name, $monitor) = @_;
resource "mackerel_monitor" "<?= $name ?>" {
name = "[<?= $env ?>] <?= $monitor->{name} ?>"
? if (defined $monitor->{memo}) {
memo = <<-EOT
<?= $monitor->{memo} ?>
EOT
? }
? if (defined $monitor->{notificationInterval}) {
notification_interval = <?= $monitor->{notificationInterval} ?>
? }
connectivity {
scopes = [
? for my $scope (@{$monitor->{scopes}}) {
"<?= $scope ?>",
? }
]
}
}
EOH
$tmts->{expression} = build_template(<<'EOH');
? my ($env, $name, $monitor) = @_;
resource "mackerel_monitor" "<?= $name ?>" {
name = "[<?= $env ?>] <?= $monitor->{name} ?>"
? if (defined $monitor->{memo}) {
memo = <<-EOT
<?= $monitor->{memo} ?>
EOT
? }
? if (defined $monitor->{notificationInterval}) {
notification_interval = <?= $monitor->{notificationInterval} ?>
? }
expression {
expression = "<?= $monitor->{expression} ?>"
operator = "<?= $monitor->{operator} ?>"
? if (defined $monitor->{warning}) {
warning = "<?= $monitor->{warning} ?>"
? }
? if (defined $monitor->{critical}) {
critical = "<?= $monitor->{critical} ?>"
? }
}
}
EOH
$tmts->{external} = build_template(<<'EOH');
? my ($env, $name, $monitor) = @_;
resource "mackerel_monitor" "<?= $name ?>" {
name = "[<?= $env ?>] <?= $monitor->{name} ?>"
? if (defined $monitor->{memo}) {
memo = <<-EOT
<?= $monitor->{memo} ?>
EOT
? }
? if (defined $monitor->{notificationInterval}) {
notification_interval = <?= $monitor->{notificationInterval} ?>
? }
external {
method = "<?= $monitor->{method} ?>"
url = "<?= $monitor->{url} ?>"
service = "<?= $monitor->{service} ?>"
response_time_critical = <?= $monitor->{responseTimeCritical} ?>
response_time_warning = <?= $monitor->{responseTimeWarning} ?>
response_time_duration = <?= $monitor->{responseTimeDuration} ?>
max_check_attempts = <?= $monitor->{maxCheckAttempts} ?>
certification_expiration_warning = <?= $monitor->{certificationExpirationWarning} ?>
certification_expiration_critical = <?= $monitor->{certificationExpirationCritical} ?>
headers = {
? for my $header (@{$monitor->{headers}}) {
<?= $header->{name} ?> = "<?= $header->{value} ?>"
? }
}
}
}
EOH
$tmts->{host} = build_template(<<'EOH');
? my ($env, $name, $monitor) = @_;
resource "mackerel_monitor" "<?= $name ?>" {
name = "[<?= $env ?>] <?= $monitor->{name} ?>"
? if (defined $monitor->{memo}) {
memo = <<-EOT
<?= $monitor->{memo} ?>
EOT
? }
? if (defined $monitor->{notificationInterval}) {
notification_interval = <?= $monitor->{notificationInterval} ?>
? }
host_metric {
metric = "<?= $monitor->{metric} ?>"
operator = "<?= $monitor->{operator} ?>"
? if (defined $monitor->{warning}) {
warning = "<?= $monitor->{warning} ?>"
? }
? if (defined $monitor->{critical}) {
critical = "<?= $monitor->{critical} ?>"
? }
duration = "<?= $monitor->{duration} ?>"
max_check_attempts = <?= $monitor->{maxCheckAttempts} ?>
scopes = [
? for my $scope (@{$monitor->{scopes}}) {
"<?= $scope ?>",
? }
]
}
}
EOH
$tmts->{service} = build_template(<<'EOH');
? my ($env, $name, $monitor) = @_;
resource "mackerel_monitor" "<?= $name ?>" {
name = "[<?= $env ?>] <?= $monitor->{name} ?>"
? if (defined $monitor->{memo}) {
memo = <<-EOT
<?= $monitor->{memo} ?>
EOT
? }
? if (defined $monitor->{notificationInterval}) {
notification_interval = <?= $monitor->{notificationInterval} ?>
? }
service_metric {
service = "<?= $monitor->{service} ?>"
metric = "<?= $monitor->{metric} ?>"
operator = "<?= $monitor->{operator} ?>"
duration = <?= $monitor->{duration} ?>
? if (defined $monitor->{warning}) {
warning = "<?= $monitor->{warning} ?>"
? }
? if (defined $monitor->{critical}) {
critical = "<?= $monitor->{critical} ?>"
? }
max_check_attempts = <?= $monitor->{maxCheckAttempts} ?>
}
}
EOH
sub build_template {
my ($tmpl) = @_;
return Text::MicroTemplate->new(
template => $tmpl,
escape_func => undef,
)->build;
}
while (my $line = <STDIN>) {
my $monitor = decode_json($line);
my $tmt = $tmts->{ $monitor->{type} };
if ($tmt) {
my $name = $monitor->{name};
$name =~ s/[\s\.]+/-/g;
$name = lc($name);
$name = sprintf("%s-%s", $monitor->{type}, $name);
print $tmt->($env, $name, $monitor);
print "\n";
}
else {
warn "unknown type: " . $monitor->{type};
}
}
=disclaimer
This script not covers all attributes of terraform resource "mackerel_monitor"
Please check terraform plan before terraform apply.
=usage
$ curl -s -H "X-Api-Key: $MACKEREL_APIKEY" 'https://api.mackerelio.com/api/v0/monitors' | jq '.monitors[]' | perl monitors.pl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment