Friday, July 1, 2016

Reporting critical events of the user's Android device via wso2 EMM


When managing devices the administrator must be notified of events occurring on the user's devices that reduce the effectiveness and efficiency of the tasks performed by a device, so that he/she can take immediate action and correct it. For example, if a cooperate application is utilizing a high CPU or memory percentage, you as the admin can stop that application from running or uninstall and re-install the application again on the user's device.

In WSO2 EMM all such events are captured and published to WSO2 Data Analytics Server (WSO2 DAS).

Android agent is capable of sending out critical events to WSO2 DAS server and new event listeners can be plugged in to the Android agent.
you can go through  here to get more detail.
 
In this section lets take a look the behavior of this feature.

1. Configure the EMM server to enable event publishing in wso2 DAS.
2. Create stream and receiver to publish events in WSo2 DAS.
1. First you need to enroll the android device in WSo2 EMM server.
2. How to trigger the alert.
3. retrieve the published events.
5. Curl command
    i.  Publish event.
    ii. Retrieve the published events.


Configure the EMM server to enable event publishing in wso2 DAS.

1. Download the WSo2 DAS from here
2. Since wso2 EMM server and wso2 DAS server is starting up in same time,port conflict issue might be occurred, so Port offset WSO2 DAS by 1 and start the server. There are two ways to set an offset to a port
  • Pass the port offset to the server during startup. The following command starts the server with the default port incremented by 1.
         ./wso2server.sh -DportOffset=1
  • Set the Ports section of the <EMM_HOME>/repository/conf/carbon.xml file as follows: <Offset>1</Offset>
    Once you have port offset start WSO2 DAS using the following command.

    ./wso2server.sh
3. Configure the device-analytics-config.xml file that is in the <EMM_HOME>/repository/conf/etc directory.

    Enable the settings by assigning true as the value within the <Enabled> tag.

    ReceiverServerURL: The URL must be in the protocol://hostname:port format. Since you port offset WSO2 DAS, make sure to update the port accordingly.
    Example: tcp://localhost:7611 if you have not port offset and tcp://localhost:7611+n if you have port offset by n.

    AdminUsername: Provide the administrator username. The default username is admin.
    AdminPassword: Provide the password of the administrator. The default password is admin.

4.  Configure the <EMM_HOME>/repository/conf/analytics/analytics-data-config.xml file to specify the data service access mode and the URL.There three types of access modes.

  • LOCAL    -  The Analytics API only accesses the AnalyticsDataServices OSGI service within itself.
  • REMOTE - The Analytics API only accesses the AnalyticsDataServices OSGI service in a remote instance. This mode is suitable when the node is a light weight node and does not contain an AnalyticsDataServices OSGi service
  • AUTO       - This is the default mode.
    The Analytics API of a DAS server node always has access to a AnalyticsDataServices OSgi service that exists within that same server node. At the same time, the same API can be used to change the mode and connect to a remote instance. This is done by setting the connection mode to Auto which allows the connection mode to be switched between LOCAL and REMOTE depending on the availability of the required AnalyticsDataServices OSgi service.
Create stream and receiver to publish events in WSo2 DAS. 
1. Create Event stream to define the data format of the streaming data.
2. Create Stream Receiver to binding stream to stores.
otherwise use carbonapp to deploy the artifact directly.

Curl command:
 i. Publish Event.
     
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 892ce0b71d30b2d5cf2c1b10806df382" -k -d '{ "deviceIdentifier": "353863072233137", "payload": "{\"packageName\":\"org.wso2.emm.agent\",\"state\":\"created\"}", "type": "APPLICATION_STATE" }' -v http://10.10.10.70:9763/mdm-android-agent/events/

Result:
POST /mdm-android-agent/events/ HTTP/1.1
User-Agent: curl/7.35.0
Host: 10.10.10.70:9763
Accept: */*
Content-Type: application/json
Authorization: Bearer 892ce0b71d30b2d5cf2c1b10806df382
Content-Length: 147
upload completely sent off: 147 out of 147 bytes


HTTP/1.1 200 OK
Date: Thu, 05 May 2016 10:50:18 GMT
Content-Type: application/json
Content-Length: 51
Server WSO2 Carbon Server is not blacklisted
Server: WSO2 Carbon Server
Connection #0 to host 10.10.10.70 left intact

{"responseCode":"Event is published successfully."}


ii. Getting Event detail of given device.

 curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 5747a79c4ec8d7445aa622463031d299" -k -v http://10.10.10.70:9763/mdm-android-agent/events/353863072233137

Result:
 GET /mdm-android-agent/events/353863072233137 HTTP/1.1
User-Agent: curl/7.35.0
Host: 10.10.10.70:9763
Accept: */*
Content-Type: application/json
Authorization: Bearer 5747a79c4ec8d7445aa622463031d299


HTTP/1.1 200 OK
Date: Thu, 05 May 2016 11:27:44 GMT
Content-Type: application/json
Content-Length: 216
Server WSO2 Carbon Server is not blacklisted
Server: WSO2 Carbon Server
Connection #0 to host 10.10.10.70 left intact
[{"values":{"_version":"1.0.0","payload":"{\"packageName\":\"org.wso2.emm.agent\",\"state\":\"created\"}","type":"APPLICATION_STATE","deviceIdentifier":"353863072233137"},"id":"a4d3ce13-e6e6-369e-91c3-c186dc844cda"}]


iii. Getting specific type of given device.

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 5747a79c4ec8d7445aa622463031d299" -k -v http://10.10.10.70:9763/mdm-android-agent/events/353863072233137/type/APPLICATION_STATE

Result:

GET /mdm-android-agent/events/353863072233137/type/APPLICATION_STATE HTTP/1.1
User-Agent: curl/7.35.0
Host: 10.10.10.70:9763
Accept: */*
Content-Type: application/json
Authorization: Bearer 5747a79c4ec8d7445aa622463031d299

HTTP/1.1 200 OK
Date: Thu, 05 May 2016 11:46:59 GMT
Content-Type: application/json
Content-Length: 431
Server WSO2 Carbon Server is not blacklisted
Server: WSO2 Carbon Server

Connection #0 to host 10.10.10.70 left intact
[{"values":{"_version":"1.0.0","payload":"{\"packageName\":\"org.wso2.emm.agent\",\"state\":\"created\"}","type":"APPLICATION_STATE","deviceIdentifier":"353863072233137"},"id":"a4d3ce13-e6e6-369e-91c3-c186dc844cda"},{"values":{"_version":"1.0.0","payload":"{\"packageName\":\"org.wso2.emm.agent\",\"state\":\"removed\"}","deviceIdentifier":"353863072233137","type":"APPLICATION_STATE"},"id":"e3d4c024-ed6d-3875-b831-247ba5845772"}] 


 iv. Getting event detail for given time period.

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 5747a79c4ec8d7445aa622463031d299" -k -v http://10.10.10.70:9763/mdm-android-agent/events/353863072233137/date?352317052330505?from=1462108907&to=1462108930 

Result: 

GET /mdm-android-agent/events/352317052330505?from=1462108907&to=1462108930 HTTP/1.1
Host: localhost:9443
User-Agent: curl/7.43.0
Accept: */*
Content-Type: application/json
Authorization: Bearer ee189258cea79c86d7656235f30e231d

HTTP/1.1 200 OK
Date: Sun, 01 May 2016 05:55:56 GMT
Content-Type: application/json
Content-Length: 476
Server: WSO2 Carbon Server
[{"values":{"_version":"1.0.0","payload":"{\"packageName\":\"com.google.android.apps.docs\",\"state\":\"dataCleared\"}",
"type":"APPLICATION_STATE","deviceIdentifier":"352317052330505"},"id":"f0c867ac-8d49-340f-aed4-ae3a34e7ec3c"},{"values":{"_version":"1.0.0","payload":"{\"packageName\":\"system_server\",\"cpu\":11,\"pid\":2436,\"pss\":0,\"sharedDirty\":0,\"uss\":0}","type":"RUNTIME_STATE",
"deviceIdentifier":"352317052330505"},"id":"b65a65c5-23f9-3258-83be-1745f4d8e0ae"}]