Search code examples
google-cloud-platformservicemonitoringuptime

How can I monitor VM services in GCP?


I have a VM instance that contains SQL service, I want to monitor this SQL service how can I do that?


Solution

  • Google provides a Cloud Monitoring agent that can be used in Compute Engine to collect more detailed metrics from the Compute Engine instance itself and third-party applications it might contain, displaying this information at the Cloud Monitoring page. Here's a list of the applications it can gather the metrics from. Here's a guide on how to install the agent.

    Assuming you're using an Ubuntu-based distro and the SQL service is MySQL perhaps this guide may be more relevant. In essence what you'd have to do is:

    1. Add the Monitoring agent's package repository:

      curl -sSO https://dl.google.com/cloudagents/add-monitoring-agent-repo.sh
      sudo bash add-monitoring-agent-repo.sh
      sudo apt-get update
      
    2. Install the Monitoring agent:

      sudo apt-get install stackdriver-agent
      
    3. Start it:

      sudo service stackdriver-agent start
      
    4. Install MySQL monitoring:

      sudo apt-get install libmysqlclient20
      
    5. Download mysql.conf and place it in the directory /opt/stackdriver/collectd/etc/collectd.d/ which is where the Monitoring agent operates on:

      (cd /opt/stackdriver/collectd/etc/collectd.d/ && sudo curl -O https://raw.githubusercontent.com/Stackdriver/stackdriver-agent-service-configs/master/etc/collectd.d/mysql.conf)
      
    6. Edit the configuration file and replace DATABASE_NAME with the name of the database you want to monitor, and any other variables that might apply to your database.

    7. Restart the Monitoring agent:

      sudo service stackdriver-agent restart
      

    Once that's done, go to the Cloud Monitoring page, select the VM instance as Resource Type: and scroll down until you see Agent Metrics to view the metrics collected by the Monitoring agent.

    Optionally you can also install a Cloud Logging agent which will ingest logs from the application. You'd install it with:

    curl -sSO https://dl.google.com/cloudagents/add-logging-agent-repo.sh
    sudo bash add-logging-agent-repo.sh
    sudo apt-get update
    
    sudo apt-get install -y google-fluentd-catch-all-config-structured
    
    sudo service google-fluentd start
    

    Keep in mind a certain pricing is applied to Cloud Logging once you pass a specific threshold of data storage so that's something to keep in mind if you don't want to accidentally incurr costs. Lastly and again, these steps are applicable if you're indeed using an Ubuntu distro and MySQL, if not, check the links and select the options that meet your configuration accordingly.