diff --git a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/datasense/PushSystemUsage.java b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/datasense/PushSystemUsage.java index df4560b4..1409bed3 100644 --- a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/datasense/PushSystemUsage.java +++ b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/datasense/PushSystemUsage.java @@ -22,6 +22,7 @@ import java.io.IOException; import org.json.JSONObject; import org.wso2.agent.common.Common; +import org.wso2.agent.config.Configuration; import org.wso2.agent.resourcemonitor.pojo.SystemUsage; import com.sun.jersey.api.client.Client; @@ -36,7 +37,8 @@ public class PushSystemUsage { try { Client client = Client.create(Common.configureClient()); client.setConnectTimeout(50000); - WebResource webResource = client.resource("https://localhost:9443/connected-lap-agent/push_data"); + String URL= String.valueOf(new Configuration().getInitProperty("https-ep")); + WebResource webResource = client.resource(URL+"/connected-lap-agent/push_data"); Form form = new Form(); form.add("deviceId", deviceid); form.add("deviceOwner", owner); diff --git a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/main/JavaApp.java b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/main/JavaApp.java index f101605a..4b650e92 100644 --- a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/main/JavaApp.java +++ b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/main/JavaApp.java @@ -72,12 +72,12 @@ public class JavaApp { Configuration config = new Configuration(); try { String token = String.valueOf(config.getInitProperty("auth-token")); - //token="9c7a1b4d6625cdd936b894cc0e7c7390"; String refreshtoken = String.valueOf(config.getInitProperty("refresh-token")); String device_name = String.valueOf(config.getInitProperty("device-name")); Client client = Client.create(Common.configureClient()); client.setConnectTimeout(50000); - WebResource webResource = client.resource("https://localhost:9443/CONNECTEDLAP/device/register"); + String URL= String.valueOf(new Configuration().getInitProperty("https-ep")); + WebResource webResource = client.resource(URL+"/CONNECTEDLAP/device/register"); String mac = spec.getMacaddress().replaceAll("\\:", ""); deviceid = mac; diff --git a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/infocollector/Collector.java b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/infocollector/Collector.java index ca798686..cbac0710 100644 --- a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/infocollector/Collector.java +++ b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/infocollector/Collector.java @@ -63,7 +63,7 @@ public class Collector { return spec; } - public SystemUsage getSysUsage(String suPassword) throws NumberFormatException, Exception { + public SystemUsage getSysUsage(String suPassword) throws Exception { SystemUsage systemUsage = new SystemUsage(); BashRunner bashRunner = new BashRunner(); systemUsage.setMacaddress(bashRunner.runScript(this.OS, 0,suPassword)); diff --git a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/systembridge/BashRunner.java b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/systembridge/BashRunner.java index 6f70ffb1..8efd4b8c 100644 --- a/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/systembridge/BashRunner.java +++ b/modules/samples/connectedlap/component/laptopagent/src/main/java/org/wso2/agent/resourcemonitor/systembridge/BashRunner.java @@ -43,29 +43,55 @@ public class BashRunner { private String[] getScript(String os, int scriptid,String suPassword) throws OperationalException { String script[] = null; try { - if (scriptid == 0) - script = new String[]{ "/bin/sh", "-c", "ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"+" | tr '\n' ' '"}; - if (scriptid == 1) - script = new String[]{ "/bin/sh", "-c", "cat /proc/cpuinfo |grep 'model name' | uniq | awk '{print substr($0, index($0,$4))}'"+" | tr '\n' ' '"}; - if(scriptid==2) - script = new String[]{ "/bin/sh", "-c", "cat /proc/cpuinfo |grep 'cpu cores' | awk '{print substr($0, index($0,$4))}' | xargs | sed -e 's/\\ /+/g' | bc"+" | tr '\n' ' '"}; - if(scriptid==3) - script = new String[]{ "/bin/sh", "-c", "echo "+suPassword+" | sudo -S dmidecode -t 17 | grep 'Size.*MB' | awk '{s+=$2} END {print s / 1024}'"+" | tr '\n' ' '"}; - if(scriptid==4) + if(os.equalsIgnoreCase("LINUX")) { + if (scriptid == 0) + script = new String[]{"/bin/sh", "-c", "ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'" + " | tr '\n' ' '"}; + if (scriptid == 1) + script = new String[]{"/bin/sh", "-c", "cat /proc/cpuinfo |grep 'model name' | uniq | awk '{print substr($0, index($0,$4))}'" + " | tr '\n' ' '"}; + if (scriptid == 2) + script = new String[]{"/bin/sh", "-c", "cat /proc/cpuinfo |grep 'cpu cores' | awk '{print substr($0, index($0,$4))}' | xargs | sed -e 's/\\ /+/g' | bc" + " | tr '\n' ' '"}; + if (scriptid == 3) + script = new String[]{"/bin/sh", "-c", "echo " + suPassword + " | sudo -S dmidecode -t 17 | grep 'Size.*MB' | awk '{s+=$2} END {print s / 1024}'" + " | tr '\n' ' '"}; + if (scriptid == 4) // script = new String[]{ "/bin/sh", "-c", "echo Amilapwd1 | sudo -S hdparm -I /dev/sda |grep 'device size with M = 1000\\*1000' | awk '{print substr($0, index($0,$9))}' | awk -F'[\t(]' '{print $2}' | awk '{print substr($1,0)}'"+" | tr '\n' ' '"}; - script = new String[]{ "/bin/sh", "-c", "echo "+suPassword+" | sudo -S fdisk -l | grep 'Disk /dev/sda' | awk '{print $3}'"+" | tr '\n' ' '"}; - if(scriptid==5) - script = new String[]{ "/bin/sh", "-c", "echo "+suPassword+" | sudo -S hdparm -I /dev/sda |grep 'device size with M = 1000\\*1000' | awk '{print $0}' | awk -F'[\t(]' '{print $2}' | awk '{print substr($1,0)}'"+" | tr '\n' ' '"}; - if(scriptid==10) - script = new String[]{ "/bin/sh", "-c", "top -b -n2 | grep 'Cpu(s)' | tail -n 1 | awk '{print $2 + $4}'"+" | tr '\n' ' '"}; - if(scriptid==11) - script = new String[]{ "/bin/sh", "-c", "free | grep Mem | awk '{print $3/$2 * 100.0}'"+" | tr '\n' ' '"}; - if(scriptid==12) - script = new String[]{ "/bin/sh", "-c", "df -h | grep -vE '^Filesystem' | awk '{ print $1\" \"$3\" \"$2 }' | grep \"/\" | awk 'NF{sum1+=$2} NF{sum2+=$3} END {print (sum1/sum2)*100}'"+" | tr '\n' ' '"}; - if(scriptid==13) - script = new String[]{ "/bin/sh", "-c", "upower -i $(upower -e | grep BAT) | grep -E 'percentage' | awk '{ print $2 }' | tr '%' ' '"+" | tr '\n' ' '"}; - if(scriptid==14) - script = new String[]{ "/bin/sh", "-c", "upower -i $(upower -e | grep BAT) | grep -E 'state' | awk '{ if($2==\"charging\"||$2==\"fully-charged\") print 1; else print 0; }'"+" | tr '\n' ' '"}; + script = new String[]{"/bin/sh", "-c", "echo " + suPassword + " | sudo -S fdisk -l | grep 'Disk /dev/sda' | awk '{print $3}'" + " | tr '\n' ' '"}; + if (scriptid == 5) + script = new String[]{"/bin/sh", "-c", "echo " + suPassword + " | sudo -S hdparm -I /dev/sda |grep 'device size with M = 1000\\*1000' | awk '{print $0}' | awk -F'[\t(]' '{print $2}' | awk '{print substr($1,0)}'" + " | tr '\n' ' '"}; + if (scriptid == 10) + script = new String[]{"/bin/sh", "-c", "top -b -n2 | grep 'Cpu(s)' | tail -n 1 | awk '{print $2 + $4}'" + " | tr '\n' ' '"}; + if (scriptid == 11) + script = new String[]{"/bin/sh", "-c", "free | grep Mem | awk '{print $3/$2 * 100.0}'" + " | tr '\n' ' '"}; + if (scriptid == 12) + script = new String[]{"/bin/sh", "-c", "df -h | grep -vE '^Filesystem' | awk '{ print $1\" \"$3\" \"$2 }' | grep \"/\" | awk 'NF{sum1+=$2} NF{sum2+=$3} END {print (sum1/sum2)*100}'" + " | tr '\n' ' '"}; + if (scriptid == 13) + script = new String[]{"/bin/sh", "-c", "upower -i $(upower -e | grep BAT) | grep -E 'percentage' | awk '{ print $2 }' | tr '%' ' '" + " | tr '\n' ' '"}; + if (scriptid == 14) + script = new String[]{"/bin/sh", "-c", "upower -i $(upower -e | grep BAT) | grep -E 'state' | awk '{ if($2==\"charging\"||$2==\"fully-charged\") print 1; else print 0; }'" + " | tr '\n' ' '"}; + }else{ + if (scriptid == 0)//mac + script = new String[]{"/bin/sh", "-c", "ifconfig en1 | awk '/ether/{print $2}'" + " | tr '\n' ' '"}; + if (scriptid == 1)//processor + script = new String[]{"/bin/sh", "-c", "sysctl -n machdep.cpu.brand_string" + " | tr '\n' ' '"}; + if (scriptid == 2)//cores + script = new String[]{"/bin/sh", "-c", "sysctl -n hw.ncpu" + " | tr '\n' ' '"}; + if (scriptid == 3)//ram size + script = new String[]{"/bin/sh", "-c", "sysctl -n hw.memsize | awk '{print $1 / 1048576}'" + " | tr '\n' ' '"}; + if (scriptid == 4)//storage +// script = new String[]{ "/bin/sh", "-c", "echo Amilapwd1 | sudo -S hdparm -I /dev/sda |grep 'device size with M = 1000\\*1000' | awk '{print substr($0, index($0,$9))}' | awk -F'[\t(]' '{print $2}' | awk '{print substr($1,0)}'"+" | tr '\n' ' '"}; + script = new String[]{"/bin/sh", "-c", "diskutil list | grep 'GUID_partition_scheme' | awk '{ print $3}' | sed 's/*//g' | awk '{s+=$0} END {print s}'" + " | tr '\n' ' '"}; + if (scriptid == 5)// + script = new String[]{"/bin/sh", "-c", "echo " + suPassword + " | sudo -S hdparm -I /dev/sda |grep 'device size with M = 1000\\*1000' | awk '{print $0}' | awk -F'[\t(]' '{print $2}' | awk '{print substr($1,0)}'" + " | tr '\n' ' '"}; + if (scriptid == 10)//processor usage + script = new String[]{"/bin/sh", "-c", "ps -A -o %cpu | awk '{s+=$1} END {print s}'" + " | tr '\n' ' '"}; + if (scriptid == 11)//memory usage + script = new String[]{"/bin/sh", "-c", "free | grep Mem | awk '{print $3/$2 * 100.0}'" + " | tr '\n' ' '"}; + if (scriptid == 12)//storage usage + script = new String[]{"/bin/sh", "-c", "df -h | grep -vE '^Filesystem' | awk '{ print $1\" \"$3\" \"$2 }' | grep \"/\" | awk 'NF{sum1+=$2} NF{sum2+=$3} END {print (sum1/sum2)*100}'" + " | tr '\n' ' '"}; + if (scriptid == 13)//batttery percentage + script = new String[]{"/bin/sh", "-c", "pmset -g batt | grep \"InternalBattery\" | awk '{print $2}' | sed 's/%;//g'" + " | tr '\n' ' '"}; + if (scriptid == 14)//charger plugged in + script = new String[]{"/bin/sh", "-c", "pmset -g batt | grep \"InternalBattery\" | awk '{print $3}' | sed 's/;//g' | awk '{ if($0==\"charging\"||$0==\"charged\") print 1; else print 0; }'" + " | tr '\n' ' '"}; + } } catch (Exception e) { }