Adding "Auto Scaling" to the Oracle Database Cloud Service - Step 2: Logging Service

This is the step 2 of adding "Auto Scaling" to the Oracle Database Cloud Service.
If you want to check the other steps, please click the below links respectively.

- Step 1: [Gather OCI DB System CPU Usage and write it into a custom log](https://qiita.com/RexZheng/items/797ab17177f1c44f2a28) 
- Step 2: [Configure OCI Logging service to ingest the custom log](https://qiita.com/RexZheng/items/f785c1337bfce82c755a)
- Step 3: [Configure OCI Service Connector Hub service to transfer the custom log to OCI Functions](https://qiita.com/RexZheng/items/5b417c1c3b80b49cd0ec)
- Step 4: [Configure OCI Functions to act accordingly based on the CPU usage in the custom log](https://qiita.com/RexZheng/items/874372ad22d18a42a3e1)

## Configure OCI Logging service to ingest the custom log

In step 1: [Gather OCI DB System CPU Usage and write it into a custom log](https://qiita.com/RexZheng/private/797ab17177f1c44f2a28), we have created and deployed a script to gather the realtime CPU usage and write it into the custom log. In the step 2, we are going to do following configuration to ingest and upload the custom logs to OCI Logging service.

- Create custom log in OCI Logging and setup Agent Configurations
- Install Oracle Unified Monitoring Agent in the target DB system node

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/5cd61acd-dc14-b8da-9608-228343721ca3.png)


## Create Custom Log in OCI Logging and setup Agent Configurations

In this section, we will create Custom Log and Agent Configuration in OCI Logging service to retrieve the logs from client side.

OCI Logging service supports lots of pre-defined [OCI services logs](https://docs.cloud.oracle.com/en-us/iaas/Content/Logging/Concepts/service_logs.htm), and the list is still growing. We don't need to do many works and we can start to use the Logging service for them easily.

But this time, the log we are going to process is totally custom, so we need to use the [Custom Logs](https://docs.cloud.oracle.com/en-us/iaas/Content/Logging/Concepts/custom_logs.htm) to help us complete this task.

### Create Custom Log

First, let's visit the Logs in the OCI Logging service.

[Figure: visit Logging service]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/0962169f-4ca8-6e47-d119-f60f1dce575e.png)

Then, click `Create Custom Log` button to start the creating.

[Figure: create Custom Logs]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/1015b65c-2c20-4fad-3540-139604353c64.png)

Creating a Custom Log is very simple, just input a meaningful name and select `COMPARTMENT` and `LOG GROUP`. If you don't want to put your logs in the `Default_Group` you can create a new one.


[Figure: create Custom Log - input name]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/d2cab8a5-ffc0-3ba4-3999-fcbdc244ace9.png)

In the additional options, we can select the expected Log Retention. We choose `1 month`, that's the default option, but if you wish to store the logs longer, OCI Logging service provide you the option of `6 months` maximumly.

If you wish even longer retention period, OCI also provide you the possibility by using Service Connector Hub and Object Storage. We can talk about this later.

[Figure: Log Retention]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/d31ac9b9-16ab-7d59-8c99-ae650d63309b.png)

Click the `Create Custom Log` button at bottom left corner, you will move to the next step `Create Agent Config`. We can create the configuration right now, or create it later and attach it to the Custom Log later.

The information you input here is quite important, because it decides the behavior the Oracle Unified Monitoring Agent in the future.

Input a meaningful name of this agent configuration, and select a compartment it belongs to.

Select `User Group` in the Choose Host Groups section, and select the user's group accordingly. You may ask the user mentioned here is which one? Well, it is the user who you added API keys to. This user owns the key and the agent will communicate with OCI via this user.

[Figure: Create Agent Configuration - 1]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/5f048e80-94e2-9a55-ea64-9559b6c12040.png)

In the Configure Log Inputs section, select `Log Path` and input a meaningful name as `INPUT NAME`. Then put the absolute path of your custom log on the target server into the  `FILE PATH` field.

[Figure: Create Agent Configuration - 2]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/e66938d6-ef2c-fdb8-2734-bdc596ea761a.png)

You may notice that there is a `Advanced Parser Options` link on this page. We can configure the parser of this agent configuration. But for now, to keep the simplicity, we will configure the parser later.

Now, click the `Create Custom Log` button again, we will be back to the Logs page and you should see the newly created Custom Log and associated Agent Configuration in the list. If you see `NONE` for the Agent Configurations, just refresh the page should be fine.

[Figure: Custom Log created]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/670c0600-8af1-69db-e2cd-0e71b7304a9b.png)

Basically, creating the Custom Log in OCI Logging service has been completed. In next section, we will install and do some configuration to the Oracle Unified Monitoring Agent. So that the agent can communicate with OCI and use the configuration to ingest and upload the custom logs onto OCI Logging service.


## Install Oracle Unified Monitoring Agent in the target DB system node

Oracle Unified Monitoring Agent can help us to ingest, parse and upload the custom logs from client side to OCI Logging service.

Oracle Unified Monitoring Agent supports Linux and Windows platform, and in the new OCI compute instances, it has been pre-installed. But as we are deploying it to the DBCS node, we need to install it manually.

### Download

For now, OCI DBCS instances are built on Oracle Linux 7.8, so we should download the [OEL7 RPM package of Oracle Unified Monitoring Agent](https://objectstorage.us-phoenix-1.oraclecloud.com/n/axmjwnk4dzjv/b/unified-monitoring-agent-ol7-repo/o/unified-monitoring-agent-0.0.5.rpm)

### Install

Once download of Oracle Unified Monitoring Agent is complete. Just perform following command to install it.

```sh
sudo yum install -y <rpm-name>
```
You should see something like this.

[Figure: install unified-monitoring-agent]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/b0048015-d5ab-e452-e275-e1ce1061e49d.png)

If you wish to, you can check the status of the Oracle Unified Monitoring Agent related services with following commands.

```sh
systemctl status unified-monitoring-agent
systemctl status unified-monitoring-agent_config_downloader.service
systemctl status unified-monitoring-agent_config_downloader.timer
systemctl status unified-monitoring-agent_restarter.path
```

You should see something like below.

[Figure: status of the agent services, right after installation]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/a2c81063-6ec0-a3a8-f021-b50e9eb48d47.png)


### Configure

To make the agent be able to communicate with OCI Logging service, we need to do some configuration.

Oracle Unified Monitoring Agent communicates with the OCI Logging service via OCI API (similar to OCI CLI), so we need to generate and configure the key pair firstly.

### Generate key pair

For our setup, we need to perform following command to generate key pair at a specific location `/etc/unified-monitoring-agent/.oci`.

```sh
cd /etc/unified-monitoring-agent
sudo mkdir .oci
cd .oci

# Generate the private key with the following commands.
# To generate the key, encrypted with a passphrase you provide when prompted:
openssl genrsa -out oci_api_key.pem -aes128 2048
# pass phrase: WhatEverYouWish

# Change the file permission to ensure that only you can read the private key file:
sudo chmod go-rwx oci_api_key.pem

# Generate the public key from your new private key:
sudo openssl rsa -pubout -in oci_api_key.pem -out oci_api_key_public.pem

# Copy the contents of the public key, you'll need to paste the value into the Console later).
sudo cat oci_api_key_public.pem
```

[Figure: Generate the private key with passphrase]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/aa728b9d-a446-fb30-acab-c82f14e17af8.png)


[Figure: Copy the public key]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/1def1900-cc43-947f-6a74-85b4e7b9c109.png)

Once you copied the content of the public key, visit OCI Web console and add it as a new API key.

[Figure: add new API key]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/2c35fe6f-b4b2-b2f6-5d6c-6f181eca27d1.png)

OCI will generate and show a fingerprint automatically on the console. And you can generate locally to make a verification.


[Figure: new API key added]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/a5fe09f9-9fdb-f226-ed30-9a348226917a.png)


```sh
# Geenrate fingerprint to verify the key is correct
sudo openssl rsa -pubout -outform DER -in oci_api_key.pem | openssl md5 -c
```

[Figure: Generate fingerprint for verification]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/93590ed7-fc59-9ae8-abc9-cf7a98845e8b.png)

Now, let's restart the Oracle Unified Monitoring Agent service and check it's log to see how it is running.

```sh
# restart the Oracle Unified Monitoring Agent
sudo systemctl restart unified-monitoring-agent

# Check the log
tail -100f /var/log/unified-monitoring-agent/unified-monitoring-agent.log
```

[Figure: Check log, configuration is empty]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/f802f597-01f0-e18b-7c75-516669e58c0b.png)

As we can see the `using configuration file` line in the log, the configuration is still empty. That's because the agent doesn't know where to grab the configuration yet.


For more information about generating OCI API key, please check [How to Generate an API Signing Key](https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#two).


### Create profile for Oracle Unified Monitoring Agent

Now, the key pair is ready. We will create a Profile for the agent, so that it knows where to go.

The profile is saved in a `config` file which should be in the same directory of the key pair `/etc/unified-monitoring-agent/.oci`.

```sh
cd /etc/unified-monitoring-agent/.oci
sudo vim config
```

Input the configuration of the profile like below. You can get the OCID of `user` and `tenancy` from the OCI Web Console. If you have installed OCI CLI, this should be quite familiar for you.

```sh
[DEFAULT]
user=<OCID of OCI USER>
fingerprint=84:52:c9:35:9e:11:a6:dc:f3:7a:fb:dd:89:4b:7b:49
key_file=/etc/unified-monitoring-agent/.oci/oci_api_key.pem
tenancy=<OCID of OCI TENANCY>
region=ap-tokyo-1
pass_phrase="<WhatEverYouWish>"

[UNIFIED_MONITORING_AGENT]
user=ocid1.user.oc1..aaaaaaaaowxmwiw75v2ib3mcclcijhdib5ofcladbz2csr4tacg766wl4exa
fingerprint=84:52:c9:35:9e:11:a6:dc:f3:7a:fb:dd:89:4b:7b:49
key_file=/etc/unified-monitoring-agent/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaro7aox2fclu4urtpgsbacnrmjv46e7n4fw3sc2wbq24l7dzf3kba
region=ap-tokyo-1
pass_phrase="<WhatEverYouWish>"

```

Save the `config` file and restart the Oracle Unified Monitoring Agent service.

```sh
sudo systemctl restart unified-monitoring-agent
```

Check the log to see what is going on.

```sh
tail -100f /var/log/unified-monitoring-agent/unified-monitoring-agent.log
```

We should see something like below. In the `using configuration file` line, there are some new content between the `<ROOT>`, that's the agent configuration just downloaded from OCI Logging service. Since I have configured two agent configurations under my user, so in the screenshot you can see two `<source>` and `<match>`, the latter one is the one we just created in OCI Logging service.

[Figure: downloaded agent configuration]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/b22f60b4-73de-a598-d84b-554c32feb885.png)

If you find that the agent configuration cannot be downloaded successfully. Highly recommend you to check the official document [Agent Troubleshooting](https://docs.cloud.oracle.com/en-us/iaas/Content/Logging/Reference/agent_troubleshooting.htm).

### View the custom log uploaded to OCI Logging service

After a while (more than 3 minutes), we could check the agent log and should be able to see that our custom log (cpu usage) has been uploaded to OCI Logging service and the response code is 200.

[Figure: agent upload log]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/d1ce2dd8-16bf-84bc-f3e5-1994ab291239.png)

Also, we should be able to see the uploaded custom log with cpu usage information in OCI Logging service.

[Figure: custom logs uploaded into OCI Logging]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/bacd8bfc-f0e3-4121-12f5-7dae863f473a.png)

If we want to check whether the log is correct, we can compare it with the original generated log on the DB System node.

[Figure: custom logs on DB System node]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/9375b93e-b165-4c46-370a-f7c0c5b68afb.png)


## Add parser to the agent configuration

Now, we can successfully ingest and upload the custom log from DB System node to the OCI Logging service. But you may notice that the log entry is just as it is in the log file. Because we have not add any parser yet. The agent doesn't know how to parse the log entry appropriately.

In this section we will add a parser to extract out the CPU Usage from each log entry.

### Add parser

Visit the Agent Configuration we just created,

[Figure: Visit Agent Configuration]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/6557d977-69a3-be49-cb92-2f8f10bd38d3.png)

Click `Edit`

[Figure: Edit the Agent Configuration]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/2ce0106b-0b23-62b7-47a1-6603c21a9783.png)

Click `Advanced Parser Options` to start adding the new parser.

[Figure: Advanced Parser Options]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/b3b31fdd-59c8-6ac2-9bbc-9c9f7c3def41.png)

Select `REGEXP` option for the PARSER, as we are going to use regular expression to parse the log entry. Input `^(?<log-time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (?<cpu-usage>.*)$` in the EXPRESSION field and `%Y-%m-%d %H:%M:%S` in the TIME FORMAT field. This expression will help us to extract the log entry time and corresponding CPU Usage.

[Figure: add REGEXP parser]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/446e3f83-76cc-0c62-5acb-118070710ca5.png)

After adding the parser to the Agent Configuration, we need to wait a while because the synchronization of the new configuration takes some time. Or you could restart the Unified Monitoring Agent service to make it faster.

### Check new configuration and parsed log entry

We can check the new agent configuration from the agent log

```sh
tail -100f /var/log/unified-monitoring-agent/unified-monitoring-agent.log
```

[Figure: Check the new agent configuration on DB System node]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/c2cecf40-ee05-f1ce-35ec-130cd288a83b.png)

In OCI Logging service, we should be able to see the parsed information `log-time` and `cpu-usage`.

[Figure: new parsed log entry in OCI Logging service]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/8010fb66-ba64-23b8-2b66-7277f8314a89.png)

## Next

[Figure: Step 2 completed]
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/100411/6129cc21-92e6-0833-4724-e43fd3b9356d.png)

Until now, we have completed the step 2: **Configure OCI Logging service to ingest the custom log**. We can see the parsed `log-time` and `cpu-usage` information in the OCI Logging service.

In next step, we will continue to complete the step 3: **Configure OCI Service Connector Hub service to transfer the custom log to OCI Functions**