Collect Apache web server metrics with the Ops Agent
Learn how to collect and monitor metrics from an Apache web server installedon a Compute Engine virtual machine (VM) instance by using the Ops Agent:
- Create a Compute Engine VM instance and install theOps Agent.
- Install an Apache web server.
- Configure the Ops Agent for the Apache web server.
- Generate traffic to the Apache web server.
- View metrics on the predefined Apache dashboard.
- Create an alerting policy.
- Test the alerting policy.
- Clean up.
To follow step-by-step guidance for this task directly in the Google Cloud console, clickGuide me:
Before you begin
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, seeDevelop applications in a constrained Google Cloud environment.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Monitoring, Cloud Logging, and OS Config APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Monitoring, Cloud Logging, and OS Config APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.
Required roles
To get the permissions that you need to create a Compute Engine instance, install Apache, and to view metric data, ask your administrator to grant you the following IAM roles on your project:
- Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) - Monitoring Editor (
roles/monitoring.editor) - OSPolicyAssignment Admin (
roles/osconfig.osPolicyAssignmentAdmin) - Service Account User (
roles/iam.serviceAccountUser) - Service Usage Admin (
roles/serviceusage.serviceUsageAdmin)
For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Create a VM instance
In the Google Cloud console, go to theVM instances page:
If you use the search bar to find this page, then select the result whose subheading isCompute Engine.
- Create a VM by clickingCreate instance. Configure your instance by using the options in the navigation menu.
- In theMachine configuration option, do the following:
- In theName field, enter a descriptive name.
- In theMachine type preset drop-down, selectShared-core > e2-small.
- Verify that theOS and storage option displaysDebian GNU/Linux. If not, click theOS and storage option and clickChange. In theBoot disk dialog, setVersion toDebian GNU/Linux.
- In theNetworking option, forFirewall, select bothAllow HTTP traffic andAllow HTTPS traffic.
- Verify that theObservability option displaysInstall Ops Agent. If not, click theObservability option and selectInstall Ops Agent for Monitoring and Logging.
- ClickCreate.
Install an Apache web server
To install an Apache web server on your Compute Engine VM instance, dothe following:
On theVM instances page, locate your new VM,go to theConnect column,and then clickSSH.
Having trouble connecting? Refer toTroubleshooting SSH.
To update the package lists, copy the following command to your clipboard,paste the command into the SSH terminal, and then press enter:
sudoapt-getupdateAfter you see the message "Reading package lists... Done", in theSSH terminal, run the following command to install an Apache2 web server:
sudoapt-getinstallapache2php7.0When asked to continue the installation, enter
Y. If theinstall command fails, then usesudo apt-get install apache2 php.When your command prompt returns, go to theVM instances page and copythe VM's external IP address into the following URL:
http://EXTERNAL_IPTo connect to your Apache web server, open a new browser tab, and thenenter the URL from the previous step.
When the web server is successfully installed, the browser tab displays theApache2 Debian default page.

Collect Apache web server logs and metrics
In these steps, you configure theOps Agentto collect logs and metrics from your Apache web server:Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to theVM instances page:
If you use the search bar to find this page, then select the result whose subheading isCompute Engine.
- Locate your new VM and then clickSSH.
Copy the following command, then paste it into the terminal for your instance, and then press enter:
#ConfiguresOpsAgenttocollecttelemetryfromtheapp.Youmustrestarttheagentfortheconfigurationtotakeeffect.set-e#Checkifthefileexistsif[!-f/etc/google-cloud-ops-agent/config.yaml];then#Createthefileifitdoesn'texist.sudomkdir-p/etc/google-cloud-ops-agentsudotouch/etc/google-cloud-ops-agent/config.yamlfi#Createabackupoftheexistingfilesoexistingconfigurationsarenotlost.sudocp/etc/google-cloud-ops-agent/config.yaml/etc/google-cloud-ops-agent/config.yaml.bak#ConfiguretheOpsAgent.sudotee/etc/google-cloud-ops-agent/config.yaml >/dev/null <<EOFmetrics:receivers:apache:type:apacheservice:pipelines:apache:receivers:-apachelogging:receivers:apache_access:type:apache_accessapache_error:type:apache_errorservice:pipelines:apache:receivers:-apache_access-apache_errorEOFThe previous command creates the configuration to collect and ingest logs and metrics from the Apache web server. For more information, seeConfigure the Ops Agent for Apache web server.
- Restart the Ops Agent:
- To restart the agent, run the following command on your instance:
sudo systemctl restart google-cloud-ops-agent
- To confirm that the agent restarted, run the following command and verify that the components "Metrics Agent" and "Logging Agent" started:
sudo systemctl status "google-cloud-ops-agent*"
- To restart the agent, run the following command on your instance:
Generate traffic
To generate traffic to your Apache web server, do the following:
Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to theVM instances page:
If you use the search bar to find this page, then select the result whose subheading isCompute Engine.
- Locate your new VM and then clickSSH.
In the SSH terminal, run the following command, which generates requeststo your Apache web server:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'- Wait two minutes for the command prompt to return or for the terminal toclose. While the command is running, HTML text is shown in the terminal.
View Apache metrics
To view theApache Overview dashboard, which is automatically created,do the following:
In the Google Cloud console, go to the Dashboards page:
If you use the search bar to find this page, then select the result whose subheading isMonitoring.
In theMy Dashboards pane, select theApache Overview dashboard from the list.

You've configured the Ops Agent to collect logs and metrics from your Apacheweb server, and you've viewed the metrics. The next step is tocreate an alerting policy so that you're notified when load on yourApache web server exceeds a threshold.
Create an email notification channel
Before you create an alerting policy, configure the notification channels that you want thealerting policy to use. Cloud Monitoring supports many different types of notificationchannels, including email, Slack, PagerDuty, and Pub/Sub. For more information, seeCreate and manage notification channels.To get notifications by e-mail, do the following:In the Google Cloud console, go to thenotifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading isMonitoring.
- In the toolbar, clickEdit Notification Channels.
- On theNotification channels page, scroll to
Email , and then clickAdd new. - Enter your email address, a display name such as
My email, and then clickSave.
Create an alerting policy
In this section, you create an alerting policy so that you are notifiedwhen the traffic to your Apache web server exceeds a defined threshold:
In the Google Cloud console, go to thenotifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading isMonitoring.
- ClickCreate policy.
Select the time series to be monitored:
- ClickSelect a metric and selectVM instance.
- In theActive metric categories list, selectApache.
- In theActive metrics list, selectworkload/apache.traffic.
- ClickApply.
The chart for Apache traffic is shown.
Advance to theConfigure trigger fields, and thenset theThreshold value field to
1500.The chart displays the threshold as a dashed line. Ensure that thedashed line is much less than the peak traffic level.
Advance to theNotifications and name fields, and then use theNotification channels menu to select your email address.
For the policy name, enter
Apache traffic above threshold.Advance to theReview alert fields, review thealerting policy, and then clickCreate policy.
Test the alerting policy
To test the alerting policy, generate traffic that exceeds the threshold:
Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to theVM instances page:
If you use the search bar to find this page, then select the result whose subheading isCompute Engine.
- Locate your new VM and then clickSSH.
In the SSH terminal, run the following command, which generates requeststo your Apache web server:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'- Wait two minutes for the command prompt to return or for the terminal toclose. While the command is running, HTML text is shown in the terminal.
When the command prompt returns, check your email for a message whosesubject line begins with
[ALERT].If you don't see an email, then examine the chart on the alerting policy toverify that the traffic level exceeded the threshold. You might need to runthe previous command again. Otherwise, wait a minute or two for the emailto arrive.
The notification provides a summary of the alerting policy and a link toan incident. Eachincident contains a record of the failure, and theseare typically helpful when troubleshooting.

You've configured the Ops Agent to collect logs and metrics from yourApache web server, but you've only viewed metrics. For informationabout how to view Apache web server logs, see the quickstartView Apache web server logs.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
If you created a new project and you no longer need the project, thendelete the project.
If you used an existing project, then do the following:
If you created a VM, then delete it:
- In the Google Cloud console, go to theVM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, clickMore actions, clickDelete, and then follow the instructions.
Delete the alerting policy that you created:
In the Google Cloud console, go to thenotifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading isMonitoring.
- Select the alerting policy that you created, and then click
Delete .
What's next
To learn about the Ops Agent and supported integrations, see:
To learn how to test whether a URL is responding to requests and tosimulate a failure, see the quickstartGet notified if your application stops responding.
To view logs from your Apache web server, see the quickstartCloud Logging for Compute Engine VMs.
To learn about charting and viewing performance data, seeDashboards overview.
For a list of metrics supported by Google Cloud, seeMetrics list.
To create your own Monitoring metrics,seeCustom metrics.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.