Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows Start Agent With EC2 Run Command #378

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ var testTypeToTestConfig = map[string][]testConfig{
{testDir: "../../../test/acceptance"},
{testDir: "../../../test/feature/windows/event_logs"},
{
testDir: "../../../test/feature/windows/userdata",
testDir: "../../../test/feature/windows/custom_start/userdata",
targets: map[string]map[string]struct{}{"os": {"win-2019": {}}},
},
{
testDir: "../../../test/feature/windows/custom_start/ssm_start",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an exception case where we only need to test starting the agent with ssm commands, I think it is fine to only run on a single os.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a sanity test the agent can start and get metrics.

targets: map[string]map[string]struct{}{"os": {"win-2019": {}}},
},
// assume role test doesn't add much value, and it already being tested with linux
Expand Down
47 changes: 41 additions & 6 deletions terraform/ec2/win/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ resource "aws_instance" "cwagent" {
vpc_security_group_ids = [module.basic_components.security_group]
associate_public_ip_address = true
instance_initiated_shutdown_behavior = "terminate"
user_data = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? data.template_file.user_data.rendered : ""
user_data = length(regexall("/feature/windows/custom_start/userdata", var.test_dir)) > 0 ? data.template_file.user_data.rendered : ""
get_password_data = true

metadata_options {
Expand All @@ -86,7 +86,7 @@ resource "aws_ssm_parameter" "upload_ssm" {
}

resource "null_resource" "integration_test_setup_agent" {
count = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? 0 : 1
count = length(regexall("/feature/windows/custom_start/userdata", var.test_dir)) > 0 ? 0 : 1
depends_on = [aws_instance.cwagent, module.validator, aws_ssm_parameter.upload_ssm]

# Install software
Expand Down Expand Up @@ -193,7 +193,7 @@ resource "null_resource" "integration_test_run" {

resource "null_resource" "integration_test_run_validator" {
# run validator only when test_dir is not passed e.g. the default from variable.tf
count = length(regexall("/feature/windows", var.test_dir)) > 0 && length(regexall("/feature/windows/userdata", var.test_dir)) < 1 ? 1 : 0
count = length(regexall("/feature/windows", var.test_dir)) > 0 && length(regexall("/feature/windows/custom_start", var.test_dir)) < 1 ? 1 : 0
depends_on = [
null_resource.integration_test_setup_agent,
null_resource.integration_test_setup_validator,
Expand Down Expand Up @@ -237,9 +237,9 @@ resource "null_resource" "integration_test_run_validator" {
}
}

resource "null_resource" "integration_test_run_validator_userdata" {
resource "null_resource" "integration_test_run_validator_start_agent_ssm" {
# run validator only when test_dir is not passed e.g. the default from variable.tf
count = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? 1 : 0
count = length(regexall("/feature/windows/custom_start/ssm_start", var.test_dir)) > 0 ? 1 : 0
depends_on = [
null_resource.integration_test_setup_validator,
null_resource.integration_test_wait,
Expand All @@ -262,12 +262,47 @@ resource "null_resource" "integration_test_run_validator_userdata" {
destination = module.validator.instance_validator_config
}

//runs validator and sets up prometheus java agent
provisioner "remote-exec" {
inline = [
"set AWS_REGION=${var.region}",
"aws ssm send-command --document-name AmazonCloudWatch-ManageAgent --parameters optionalConfigurationLocation=${local.ssm_parameter_name} --targets Key=tag:Name,Values=cwagent-integ-test-ec2-windows-${var.test_name}-${module.common.testing_id}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ssm command to start the agent with the config file uploaded to ssm parameters

]
}
}

resource "null_resource" "integration_test_run_validator_custom_start" {
# run validator only when test_dir is not passed e.g. the default from variable.tf
count = length(regexall("/feature/windows/custom_start", var.test_dir)) > 0 ? 1 : 0
depends_on = [
null_resource.integration_test_setup_validator,
null_resource.integration_test_wait,
null_resource.integration_test_run_validator_start_agent_ssm
]

connection {
type = "winrm"
user = "Administrator"
password = rsadecrypt(aws_instance.cwagent.password_data, local.private_key_content)
host = aws_instance.cwagent.public_dns
}

provisioner "file" {
source = module.validator.agent_config
destination = module.validator.instance_agent_config
}

provisioner "file" {
source = module.validator.validator_config
destination = module.validator.instance_validator_config
}

//runs validator and sets up prometheus java agent
provisioner "remote-exec" {
inline = [
"set AWS_REGION=${var.region}",
"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=true",
"powershell.exe \"& \"C:ProgramFiles\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1\" -m ec2 -a status\"",
"powershell.exe \"& 'C:ProgramFiles\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1' -m ec2 -a status\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not use double quotes here to get status check

"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=false"
]
}
Expand Down
216 changes: 216 additions & 0 deletions test/feature/windows/custom_start/userdata/agent_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
{
"agent": {
"debug": true
},
"metrics": {
"namespace": "CloudWatchAgentWinFeature",
"metrics_collected": {
"Processor": {
"measurement": [
{
"name":"% Idle Time",
"rename": "Idle_Time"
},
{
"name":"% Interrupt Time",
"rename": "Interrupt_Time"
},
{
"name":"% User Time",
"rename": "User_Time"
},
{
"name":"% Processor Time",
"rename": "Processor_Time"
}
],
"resources": [
"*"
],
"metrics_collection_interval": 1
},
"LogicalDisk": {
"measurement": [
{
"name":"% Free Space",
"rename":"Free_Space"
}
],
"resources": [
"*"
],
"metrics_collection_interval": 1
},
"PhysicalDisk": {
"measurement": [
{
"name":"% Disk Time",
"rename": "Disk_Time"
},
{
"name":"Disk Write Bytes/sec",
"rename": "Write_Bytes_Per_Sec"
},
{
"name":"Disk Read Bytes/sec",
"rename": "Read_Bytes_Per_Sec"
},
{
"name":"Disk Writes/sec",
"rename": "Write_Per_Sec"
},
{
"name":"Disk Reads/sec",
"rename": "Disk_Read_Per_Sec"
}
],
"resources": [
"*"
],
"metrics_collection_interval": 1
},
"Network Interface": {
"measurement": [
{
"name":"Bytes Sent/sec",
"rename": "Bytes_Sent_Per_Sec"
},
{
"name":"Bytes Received/sec",
"rename": "Bytes_Received_Per_Sec"
},
{
"name":"Packets Sent/sec",
"rename": "Packets_Sent_Per_Sec"
},
{
"name":"Packets Received/sec",
"rename": "Packets_Received_Per_Sec"
}
],
"resources": [
"*"
],
"metrics_collection_interval": 1
},
"Memory": {
"measurement": [
{
"name": "Available Bytes",
"rename": "Available_Bytes"
},
{
"name":"Cache Faults/sec",
"rename": "Cache_Faults_Per_Sec"
},
{
"name":"Page Faults/sec",
"rename": "Page_Faults_Per_sec"
},
{
"name":"Pages/sec",
"rename":"Page_Per_Sec"
}
],
"metrics_collection_interval": 1
},
"System": {
"measurement": [
{
"name": "Processor Queue Length",
"rename": "Processor_Queue_Length"
}
],
"metrics_collection_interval": 1
},
"TCPv4": {
"measurement": [
{
"name": "Connections Established",
"rename": "Connections_Established"
}
],
"metrics_collection_interval": 1
},
"TCPv6": {
"measurement": [
{
"name": "Connections Established",
"rename": "Connections_Established"
}
],
"metrics_collection_interval": 1
},
"procstat": [
{
"exe": "amazon-cloudwatch-agent",
"measurement": [
"cpu_usage",
"memory_rss"
],
"metrics_collection_interval": 1
}
]
},
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"force_flush_interval": 30
},
"logs": {
"logs_collected": {
"windows_events": {
"collect_list": [
{
"event_name": "Security",
"event_levels": [
"INFORMATION",
"WARNING",
"ERROR",
"CRITICAL",
"VERBOSE"
],
"log_group_name": "{instance_id}",
"log_stream_name": "SecurityEvent"
},
{
"event_name": "System",
"event_levels": [
"INFORMATION",
"WARNING",
"ERROR",
"CRITICAL",
"VERBOSE"
],
"log_group_name": "{instance_id}",
"log_stream_name": "System"
},
{
"event_name": "Application",
"event_levels": [
"INFORMATION",
"WARNING",
"ERROR",
"CRITICAL",
"VERBOSE"
],
"log_group_name": "{instance_id}",
"log_stream_name": "Application"
}
]
},

"files": {
"collect_list": [
{
"file_path": "C:/Users/Administrator/AppData/Local/Temp/test1.log",
"log_group_name": "{instance_id}",
"log_stream_name": "test1.log",
"timezone": "UTC"
}
]
}
},
"force_flush_interval": 5
}
}
Loading