usage-metrics-api

To report usage metrics of different resources on a machine

License

License

MIT
Categories

Categories

Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

com.foilen
ArtifactId

ArtifactId

usage-metrics-api
Last Version

Last Version

1.4.4
Release Date

Release Date

Type

Type

jar
Description

Description

usage-metrics-api
To report usage metrics of different resources on a machine
Project URL

Project URL

https://github.com/foilen/usage-metrics
Source Code Management

Source Code Management

https://github.com/foilen/usage-metrics

Download usage-metrics-api

How to add to project

<!-- https://jarcasting.com/artifacts/com.foilen/usage-metrics-api/ -->
<dependency>
    <groupId>com.foilen</groupId>
    <artifactId>usage-metrics-api</artifactId>
    <version>1.4.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.foilen/usage-metrics-api/
implementation 'com.foilen:usage-metrics-api:1.4.4'
// https://jarcasting.com/artifacts/com.foilen/usage-metrics-api/
implementation ("com.foilen:usage-metrics-api:1.4.4")
'com.foilen:usage-metrics-api:jar:1.4.4'
<dependency org="com.foilen" name="usage-metrics-api" rev="1.4.4">
  <artifact name="usage-metrics-api" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.foilen', module='usage-metrics-api', version='1.4.4')
)
libraryDependencies += "com.foilen" % "usage-metrics-api" % "1.4.4"
[com.foilen/usage-metrics-api "1.4.4"]

Dependencies

compile (8)

Group / Artifact Type Version
com.google.guava : guava jar 30.1-jre
ch.qos.logback : logback-classic jar 1.2.3
com.foilen : jl-smalltools jar 1.4.0
com.fasterxml.jackson.core : jackson-databind jar 2.11.1
javax.xml.bind : jaxb-api jar 2.3.1
org.apache.commons : commons-lang3 jar 3.10
org.slf4j : slf4j-api jar 1.7.30
org.springframework : spring-web jar 5.2.7.RELEASE

Project Modules

There are no modules declared in this project.

About

To report usage metrics of different resources on a machine.

License: The MIT License (MIT)

Projects

  • usage-metrics-agent: The agent that runs on all the machines to grab the metrics and to send them to the central service.
  • usage-metrics-central: The web service that receives the metrics and generate reports by user.

Usage

Host API Key

The central application has a hostKeySalt that is used to generate the key. Like that, it is possible to add new hosts by just letting them start sending information without having to whitelist them (if they have the derived key, then they are allowed).

If you do not provide a hostKeySalt, no key is needed.

It is using sha256(hostKeySalt + hostname).

Example:

  • hostKeySalt: 37D5C
  • hostname: h1.example.com
  • hostnameKey: 22f606813cd8b680aed4282cdf4fe357b435ddd2777dc5619c658de56461f886

To generate it in Linux:

echo -n 37D5Ch1.example.com | sha256sum

Compile

./create-local-release.sh

MongoDB for testing

# Execute
docker run --rm --detach \
  --publish 27017:27017 \
  --name usage-mongo \
   mongo:4.0.12

# MongoShell
docker exec -ti usage-mongo mongo
	use usage
	show collections

# Add API user (test / test)
docker exec -i usage-mongo mongo << _EOF
  use usage
  db.apiUser.update({_id: 'test'}, 
  	{_id: 'test', authPasswordHash: '$(echo -n test | sha256sum | cut -d' ' -f 1)'}, 
  	{upsert: true})
_EOF

# Show all users
docker exec -i usage-mongo mongo << _EOF
  use usage
  db.apiUser.find()
_EOF

# Add owner
docker exec -i usage-mongo mongo << _EOF
  use usage
  db.ownerMapping.update({_id: 'infra'}, 
  	{_id: 'infra', ownerUsageResourceMappings: [] }, 
  	{upsert: true})
_EOF

# Add Mapping
docker exec -i usage-mongo mongo << _EOF
  use usage
  db.ownerMapping.update({_id: 'infra'}, 
  	{ \$push: {ownerUsageResourceMappings: {type: 'localDisk', owner: 'infra_web'} } }
  )
_EOF
 
# Show all Mapping 
docker exec -i usage-mongo mongo << _EOF
  use usage
  db.ownerMapping.find().pretty()
_EOF

Agent

# Config
cat > _agent-config.json << _EOF
{
  "centralUri" : "https://usage.example.com",
  "hostname" : "h1.example.com",
  "hostnameKey" : "78459150bd7a263e154d103a98ff4405c9b48ce7b4d0e15710570f9852edad73",

  "diskSpaceRootFs" : "/hostfs/",
  
  "jamesDatabases" : [
  	{
	  	"host" : "127.0.0.1",
	  	"port" : 3306,
	  	"dbName" : "james",
	  	"dbUser" : "james",
	  	"dbPassword" : "qwerty"
	  }
  ]
}
_EOF

# Start
docker run -ti --rm \
  --volume $PWD:/local \
  --volume /:/hostfs \
  --volume /usr/bin/docker:/usr/bin/docker \
  --volume /usr/lib/x86_64-linux-gnu/libltdl.so.7.3.1:/usr/lib/x86_64-linux-gnu/libltdl.so.7 \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --workdir /local \
  usage-metrics-agent:master-SNAPSHOT /local/_agent-config.json

Central

# Config
cat > _central-config.json << _EOF
{
  "hostKeySalt" : "KFJeufjf773jf",

  "mongoUri" : "mongodb://172.17.0.1:27017/usage"
}
_EOF

# Start
docker run -ti --rm \
  --publish 8080:8080 \
  --volume $PWD:/local \
  --workdir /local \
  usage-metrics-central:master-SNAPSHOT /local/_central-config.json

Get the report

curl http://test:[email protected]:8080/report/showReport \
	--header "Content-Type: application/json" \
	-X POST --data '{}' ; echo

Swagger

You can see the API documentation here: http://localhost:8080/swagger-ui/index.html

Test the API

In Eclipse, run TestApiApp.launch

com.foilen

Versions

Version
1.4.4