<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Eval a distinct count in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64937#M16041</link>
    <description>&lt;P&gt;I'm thinking the "stats" section right at the end is the problem. Try using values() instead of count() to display freeleases and see if anything comes out:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval freeleases=100-distinctCount | stats values(freeleases) as "Free Leases"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 25 Aug 2011 20:31:39 GMT</pubDate>
    <dc:creator>acdevlin</dc:creator>
    <dc:date>2011-08-25T20:31:39Z</dc:date>
    <item>
      <title>Eval a distinct count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64934#M16038</link>
      <description>&lt;P&gt;I've been trying to determine the # of free dhcp leases.&lt;/P&gt;

&lt;P&gt;I can calculate the total current leases with:&lt;/P&gt;

&lt;P&gt;index=os host=dhcp* source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i).*? (?P&lt;SRC_IP&gt;\d+.\d+.\d+.\d+)(?= )" | chart dc(src_ip)&lt;/SRC_IP&gt;&lt;/P&gt;

&lt;P&gt;But when I pipe this into an eval statement to subtract the dc of src_ip from the total # of free leases I always wind up with 0. Which means I'm doing something wrong.&lt;/P&gt;

&lt;P&gt;index=os host=dhcp* source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i).*? (?P&lt;SRC_IP&gt;\d+.\d+.\d+.\d+)(?= )" | chart dc(src_ip) | eval freeleases = 100 - src_ip | stats c(freeleases) as "Free Leases"&lt;/SRC_IP&gt;&lt;/P&gt;

&lt;P&gt;Also tried utilizing a subsearch but I receive an error that dc can't be used w/the eval function:&lt;/P&gt;

&lt;P&gt;index=os host=dhcp* source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i).&lt;EM&gt;? (?P&lt;SRC_IP&gt;\d+.\d+.\d+.\d+)(?= )" | eval freeleases = 100 - [search src_ip=&lt;/SRC_IP&gt;&lt;/EM&gt; | chart dc(src_ip)] | stats c(freeleases) as "Free Leases"&lt;/P&gt;

&lt;P&gt;Can the wise splunk jedis help this padawan learner?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:50:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64934#M16038</guid>
      <dc:creator>Blu3fish</dc:creator>
      <dc:date>2020-09-28T09:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a distinct count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64935#M16039</link>
      <description>&lt;P&gt;I'm not exactly a Splunk guru myself and I don't currently live in a mud hut on Dagobah, but I do have a suggestion. Try using "eventstats" instead and saving the distinct count as its own field&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=os host=dhcp source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i).? (?P&amp;lt;src_ip&amp;gt;d+.d+.d+.d+)(?= )" | eventstats dc(src_ip) as "distinctCount" | eval freeleases = 100 - distinctCount | stats c(freeleases) as "Free Leases"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also, depending on exactly what you're trying to see from the field "freeleases", you might want values() or sum() instead of count().&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2011 18:28:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64935#M16039</guid>
      <dc:creator>acdevlin</dc:creator>
      <dc:date>2011-08-25T18:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a distinct count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64936#M16040</link>
      <description>&lt;P&gt;Changed my rex up a bit and while:&lt;/P&gt;

&lt;P&gt;index=os host=dhcp* source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i)lease (?P&lt;LEASE_IP&gt;[^ ]+)" | dedup lease_ip | search lease_ip=192.168.201* | eventstats dc(lease_ip) as "distinctCount" &lt;/LEASE_IP&gt;&lt;/P&gt;

&lt;P&gt;does return the correct # of leased IPs&lt;/P&gt;

&lt;P&gt;| eval freeleases = 100 - distinctCount | stats c(freeleases) as "Free Leases"&lt;/P&gt;

&lt;P&gt;returns the same result of leased IPs&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:50:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64936#M16040</guid>
      <dc:creator>Blu3fish</dc:creator>
      <dc:date>2020-09-28T09:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a distinct count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64937#M16041</link>
      <description>&lt;P&gt;I'm thinking the "stats" section right at the end is the problem. Try using values() instead of count() to display freeleases and see if anything comes out:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval freeleases=100-distinctCount | stats values(freeleases) as "Free Leases"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2011 20:31:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64937#M16041</guid>
      <dc:creator>acdevlin</dc:creator>
      <dc:date>2011-08-25T20:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a distinct count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64938#M16042</link>
      <description>&lt;P&gt;eventstats was the right direction. But when we c(freeleases) it was counting every instance of distinctCount as freeleases. To fix this I just changed &lt;/P&gt;

&lt;P&gt;stats c(freeleases) as "Free Leases"&lt;/P&gt;

&lt;P&gt;to&lt;/P&gt;

&lt;P&gt;stats max(freeleases) as "Free Leases"&lt;/P&gt;

&lt;P&gt;so the final search, for all interested parties:&lt;/P&gt;

&lt;P&gt;index=os host=dhcp* source="/var/lib/dhcp3/dhcpd.leases" | rex "(?i)lease (?P&lt;LEASE_IP&gt;[^ ]+)" | dedup lease_ip | search lease_ip=192.168.201* | eventstats dc(lease_ip) as "distinctCount" | eval freeleases =  100 - distinctCount | stats max(freeleases) as "Free Leases"&lt;/LEASE_IP&gt;&lt;/P&gt;

&lt;P&gt;(I'm specifically curious about the 192.168.201 subnet above, you can nix that bit and search over all your subnets)&lt;/P&gt;

&lt;P&gt;so problem solved! Thank you acdevlin for your help. W/o that mention of eventstats I would've been stuck in the mud.&lt;/P&gt;

&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:50:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-distinct-count/m-p/64938#M16042</guid>
      <dc:creator>Blu3fish</dc:creator>
      <dc:date>2020-09-28T09:50:12Z</dc:date>
    </item>
  </channel>
</rss>

