DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Automated Bug Fixing: From Templates to AI Agents
  • Dynamic File Upload Component in Salesforce LWC
  • Safeguarding Web Applications With Cloud Service Providers: Anti-CSRF Tokenization Best Practices
  • Make Your Backstage Templates Resilient

Trending

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • AI-Driven Test Automation Techniques for Multimodal Systems
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • Contextual AI Integration for Agile Product Teams

Creating TICKscript Templates for Kapacitor

Customers always ask me the best way to create TICKscripts for alerts and downsamples. So, I thought I'd take a moment here to provide you with some templates!

By 
Dave Patton user avatar
Dave Patton
·
Nov. 08, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.5K Views

Join the DZone community and get the full member experience.

Join For Free

Kapacitor is an integral piece of the InfluxData platform. And in fact, as the platform continues to develop, we are looking to Kapacitor to do more and more in terms of data processing and workloads. At the core of these workloads are alerts and downsampling or aggregations. I am often asked by customers what the best way to create TICKscripts for these two types of workloads is. So, I thought I would take a moment here to provide you with some templates you can use as starting points for an alert and a downsample.

Alerts

Alerts are at the core of what Kapacitor does and are the most common type of workload for Kapacitor in the wild. I'm going to let you in on a secret for creating great alert scripts: Use Chronograf to stub them out. Chronograf has the ability to visually create alerts.
In the image above, I have created an alert on CPU usage and set a threshold of 80%. When the alert triggers, I want to it make an HTTP POST. Pretty simple alert.

Once you have created your alert, you can view and copy the actual TICKscript. Go back to the Alert Rules page and select the Edit TICKscript button for your alert.
Prior versions will just show the script contents, but if you are using the latest version of Chronograf (at least 1.3.10), this will open up the new TICKscript editor, which you can use to start writing more TICK code.

Downsamples

The second most common workload for Kapacitor is using it to downsample or aggregate data. In the Kapacitor docs, there is a guide for using Kapacitor as a Continuous Query Engine, but it's not exactly clear whether this is the same thing as aggregation and downsampling. So, let's discuss it here as well. In general, our recommendation is to run aggregation or downsampling TICK scripts as BATCH scripts. The script snippet below will run the query every five minutes and grab the last five minutes worth of CPU data from our Telegraf database. It will then take the mean of that data and store it back into the DB. Assuming that Telegraf is reporting every 30 seconds, this basically says that we are going to downsample from 30-second resolution to five-minute resolution data:

batch
   |query('SELECT mean(usage_idle) as usage_idle FROM "telegraf"."autogen".cpu')
      .period(5m)
      .every(5m)
      .groupBy(*)
   |influxDBOut()
      .database('telegraf')
      .retentionPolicy('autogen')
      .measurement('cpu_idle_5minute')
      .precision('s')

What if I wanted to downsample from 30 seconds to, say, an hour? In that case, your script should look like this:

batch
   |query('SELECT mean(usage_idle) as usage_idle FROM "telegraf"."autogen".cpu')
      .period(1h)
      .every(1h)
      .groupBy(*)
   |influxDBOut()
      .database('telegraf')
      .retentionPolicy('autogen')
      .measurement('cpu_idle_1hour')
      .precision('s')

You can see that the general rule for downsampling is that the period() should equal every().

Conclusion

We have now given you some templates to use for creating TICKscripts for the two most common Kapacitor use cases: alerts and downsampling. Of course, there is a lot more you can do with Kapacitor. As a best practice, when developing a lot of TICKscripts, I would highly recommend that you set up a GitHub repo and/or a Wiki for folks to use to get these templates and to use as a guide for different scripts. It's also helpful for dashboard definitions — but that is for another article.

Now go write some code!

Template

Published at DZone with permission of Dave Patton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Automated Bug Fixing: From Templates to AI Agents
  • Dynamic File Upload Component in Salesforce LWC
  • Safeguarding Web Applications With Cloud Service Providers: Anti-CSRF Tokenization Best Practices
  • Make Your Backstage Templates Resilient

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: